File: ch02.html

package info (click to toggle)
capisuite 0.4.5-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,588 kB
  • ctags: 1,050
  • sloc: cpp: 3,979; sh: 3,465; python: 446; makefile: 263
file content (158 lines) | stat: -rw-r--r-- 11,826 bytes parent folder | download | duplicates (4)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 2. Users Guide</title><meta name="generator" content="DocBook XSL Stylesheets V1.65.1" /><link rel="home" href="index.html" title="CapiSuite 0.4.5" /><link rel="up" href="index.html" title="CapiSuite 0.4.5" /><link rel="previous" href="re05.html" title="capisuitefax" /><link rel="next" href="ch02s02.html" title="A first look on the incoming and idle scripts" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 2. Users Guide</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="re05.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch02s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="userguide"></a>Chapter 2. Users Guide</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch02.html#ug_python">Introduction to Python</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch02.html#python_basics">Python Basics</a></span></dt><dt><span class="sect2"><a href="ch02.html#py_blocks">Blocks, Functions and Exceptions</a></span></dt><dt><span class="sect2"><a href="ch02.html#py_modules">Working with modules</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02s02.html">A first look on the incoming and idle scripts</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch02s02.html#ug_scripts_incoming">The incoming script</a></span></dt><dt><span class="sect2"><a href="ch02s02.html#ug_scripts_idle">The idle script</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02s03.html">Used file formats</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch02s03.html#voice_fileformat">Format of voice files (inversed A-Law, 8kHz, mono)</a></span></dt><dt><span class="sect2"><a href="ch02s03.html#fax_fileformat">Format of fax files (Structured Fax Files)</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02s04.html">Tutorial: writing an incoming script</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch02s04.html#incoming_tut_basics">Basics and a really dumb answering machine.</a></span></dt><dt><span class="sect2"><a href="ch02s04.html#incoming_tut_improving">Improving it to a useful (?) state</a></span></dt><dt><span class="sect2"><a href="ch02s04.html#incoming_tut_unique_names">Using sensible file names</a></span></dt><dt><span class="sect2"><a href="ch02s04.html#incoming_tut_fax_recognition">Automatic fax recognition and receiving</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02s05.html">Example for an idle script</a></span></dt><dt><span class="sect1"><a href="ch02s06.html">Structural overview of the default scripts</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch02s06.html#default_incoming">incoming.py</a></span></dt><dt><span class="sect2"><a href="ch02s06.html#default_idle">idle.py</a></span></dt><dt><span class="sect2"><a href="ch02s06.html#default_capisuitefax">capisuitefax</a></span></dt><dt><span class="sect2"><a href="ch02s06.html#default_helpers">cs_helpers.py</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02s07.html">CapiSuite command reference</a></span></dt></dl></div><p>In the last chapter you've seen how to use the default scripts distributed with <span class="application">CapiSuite</span>.
		But the main goal in developing <span class="application">CapiSuite</span> was not to provide a perfect ready-to-use
		application. I intended to develop a tool where you can write your <span class="emphasis"><em>own</em></span>
		applications very easyly. I'll show you how to do this in the next sections.</p><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="ug_python"></a>Introduction to Python</h2></div></div><div></div></div><p>As I thought about the scripting language I wanted to integrate into <span class="application">CapiSuite</span>,
		my first idea was to develop an own, simple one. But as more as I looked into it, the
		more I found that a general purpose language will be much
		more helpful than re-inventing every wheel that I would need. So I looked for some
		easy to integrate (and to learn) language. The one I liked most was Python - and it
		also had a nice documentation about embedding, so I chose it and I'm still happy about
		that decision. :-)</p><p>So the first thing you'll have to do is to learn Python. Don't be afraid -
		it was developed as a beginners language and Guido (Guido van Rossum, the inventor of Python)
		has done very well in my opinion.</p><p>In the next few sections, I'll give you a short introduction to the features of Python
		you most probably will need for <span class="application">CapiSuite</span>. As this shouldn't be a manual about Python or a tutorial
		in computer programming, I assume you're already familiar with the basic concepts of todays
		wide-spread procedural and object-oriented languages.</p><p>If not, I would advise you to get and read a book for learning Python - there are many
		available in different languages. The Python home page on <a href="http://www.python.org" target="_top">http://www.python.org</a>
		has also nice and comprehensive manuals and tutorials available for free.</p><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="python_basics"></a>Python Basics</h3></div></div><div></div></div><p>Python supports most features you know from other common languages. Here's the
		syntax of the basic operations shown in a Python session. A Python session is another
		fine feature of its interpreter: just start it by typing <span><b class="command">python</b></span>
		in a shell and you'll get a prompt:</p><pre class="screen">gernot@linux:~&gt; python
Python 2.2.1 (#1, Sep 10 2002, 17:49:17)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
&gt;&gt;&gt;</pre><p>As you can see, the Python prompt is <tt class="literal">&gt;&gt;&gt;</tt>.
		If you enter commands that span multiple lines, Python shows a second
		prompt: <tt class="literal">...</tt></p><pre class="screen">&gt;&gt;&gt; if (1==2):
...     print "Now THAT's interesting!"
...</pre><p>Ok, now let's go on:</p><pre class="screen">&gt;&gt;&gt; # comments start with # at the begin of a line
&gt;&gt;&gt; # now the usual first steps
&gt;&gt;&gt; print "hello world"
hello world
&gt;&gt;&gt; # variables
&gt;&gt;&gt; a=5 # no separate declarations necessary
&gt;&gt;&gt; b=a*2
&gt;&gt;&gt; print b
10
&gt;&gt;&gt; b='hello'
&gt;&gt;&gt; print b,'world'
hello world
&gt;&gt;&gt; # python is very powerful in handling sequences
&gt;&gt;&gt; a=(1,2,3) # defines a tuple (not changeable!)
&gt;&gt;&gt; print a
(1, 2, 3)
&gt;&gt;&gt; a[1]=2  # this must fail
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in ?
TypeError: object doesn't support item assignment
&gt;&gt;&gt; a=[1,2,3] # defines a list (changeable)
&gt;&gt;&gt; a[1]=7
&gt;&gt;&gt; print a
[1, 7, 3]
&gt;&gt;&gt; # control structures
&gt;&gt;&gt; if (b=='hello'):
...     print "b is hello"
... else:
...     print "????"
...
b is hello
&gt;&gt;&gt; # the for statement can iterate over sequences
&gt;&gt;&gt; for i in a:
...     print i
...
1
7
3
&gt;&gt;&gt; # replace positions 1 to 3 (without 3) with 0
&gt;&gt;&gt; a[1:3]=[0]
&gt;&gt;&gt; a
[1, 0]
&gt;&gt;&gt; # a[-i] is the i-the element counted from the back
&gt;&gt;&gt; a[-1]=7; a[-2]=8
&gt;&gt;&gt; a
[8, 7]</pre></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="py_blocks"></a>Blocks, Functions and Exceptions</h3></div></div><div></div></div><p>Blocks are grouped only by identation. No <tt class="literal">begin</tt>,
			<tt class="literal">end</tt>, braces (<tt class="literal">{</tt>, <tt class="literal">}</tt>)
			or the like are needed. This sounds very uncomfortable at the first sight, but it's
			really nice - you must always structure your code exactly how you
			<span class="emphasis"><em>mean</em></span> it:</p><pre class="screen">&gt;&gt;&gt; for i in [1,2,3]:
...     print 2*i
...
2
4
6
&gt;&gt;&gt; i=0
&gt;&gt;&gt; while (i!=3):
...     print i
...     i+=1
...
0
1
2</pre><p>Now let's see how to define functions and how to work with
			exceptions:</p><pre class="screen">&gt;&gt;&gt; def double_it(a):
...     return (2*a)
...
&gt;&gt;&gt; print double_it(9)
18
&gt;&gt;&gt; print double_it("hello")
hellohello
&gt;&gt;&gt;
&gt;&gt;&gt; # let's trigger a exception
&gt;&gt;&gt; a=1/0
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in ?
ZeroDivisionError: integer division or modulo by zero
&gt;&gt;&gt;
&gt;&gt;&gt; # now let's catch it
&gt;&gt;&gt; try:
...     a=1/0
... except ZeroDivisionError,e:
...     print "You divided by zero, message was:",e
...
You divided by zero, message was: integer division or modulo by zero</pre></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="py_modules"></a>Working with modules</h3></div></div><div></div></div><p>Modules are a way to group functions together. They must be
			imported before you can use them and they give you a new object
			containing all functions. Let's play around with some of them:
			</p><pre class="screen">&gt;&gt;&gt; import time
&gt;&gt;&gt; # what is in time?
&gt;&gt;&gt; dir(time)
['__doc__', '__file__', '__name__', 'accept2dyear', ...]
&gt;&gt;&gt; # So - what do all these functions do? Python can tell...
&gt;&gt;&gt; print time.__doc__
This module provides various functions to manipulate time values.

[...]

Variables:

[...]

Functions:

time() -- return current time in seconds since the Epoch as a float
ctime() -- convert time in seconds to string
[...]

&gt;&gt;&gt; # Could you please explain ctime in more detail?
&gt;&gt;&gt; print time.ctime.__doc__
ctime(seconds) -&gt; string

Convert a time in seconds since the Epoch to a string in local time.
This is equivalent to asctime(localtime(seconds)). When the time tuple
is not present, current time as returned by localtime() is used.

&gt;&gt;&gt; time.time()
1044380131.186987
&gt;&gt;&gt; time.ctime()
'Tue Feb  4 18:35:36 2003'
&gt;&gt;&gt; import os
&gt;&gt;&gt; os.getuid()
500
&gt;&gt;&gt; import pwd
&gt;&gt;&gt; pwd.getpwuid(500)
('hans', 'x', 500, 100, 'Hans Meier', '/home/hans', '/bin/bash')</pre><p>Ok, now I hope you got a small idea of Python. Have fun with
			it. I had... :-)</p><p>If you have further questions, I would <span class="emphasis"><em>really</em></span> advise you
			to continue with a good book or the documentation on
			<a href="http://www.python.org" target="_top">http://www.python.org</a>. Please don't ask general Python
			questions on the CapiSuite lists...</p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="re05.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch02s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">capisuitefax </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> A first look on the incoming and idle scripts</td></tr></table></div></body></html>