Re: [Tutor] PDF to TXT

2011-01-23 Thread Hongbao Chen
ubject: Re: [Tutor] Telephone app
Message-ID:

Content-Type: text/plain; charset=ISO-8859-1

> Can you step back a bit and explain what it is you are trying to
> accomplish? "flow through" and "functional flow through" are meaningless
> terms in telecomms - at least so far as I am aware (after my 35 years in
> telecomms engineering...)


It's two fold. First is the obvious of conducting a call/receiving the
caller id info. The second is to send directly to the phone, and
transmit the caller id data.

By flow through, I mean that the phone has one of 2 states(on the
hook, off the hook), and three sub states(on the hook/off the hook in
use/off the hook not in use).

On the actual phone we pick up the receiver, or press the button on
the cordless to receive, but the line is always connected, meaning it
stops at the phone(terminal). I pick up the receiver, and transmit a
series of specific tones which indicate the area code, trunk number
and extension(if I remember this correctly, it's been a while since I
studied the phone itself).

So I have to receive the signal that the phone is ringing(then I'm
assuming it sends the caller id info in between rings in some form)/or
transmit a series of tones to them to connect.

So I think my main question is what modules might be relevant to doing
this? And should I be thinking of it any differently than a USB port
which has 4 pins two data(+-), and two dc current(+-)?


--

Message: 4
Date: Sun, 23 Jan 2011 21:33:33 +
From: Walter Prins 
To: David Hutto 
Cc: Alan Gauld , tutor@python.org
Subject: Re: [Tutor] Telephone app
Message-ID:

Content-Type: text/plain; charset="iso-8859-1"

On 23 January 2011 21:04, David Hutto  wrote:

> So I have to receive the signal that the phone is ringing(then I'm
> assuming it sends the caller id info in between rings in some form)/or
> transmit a series of tones to them to connect.
>
> So I think my main question is what modules might be relevant to doing
> this? And should I be thinking of it any differently than a USB port
> which has 4 pins two data(+-), and two dc current(+-)?
>
>
I think you're thinking too low level, as alluded to by Alan this type of
stuff is done via a voice-modem that you can directly control (via serial
port) and get signals from e.g. using its command set.  A common standard
for about 3 decades has been the Hayes command set:
http://en.wikipedia.org/wiki/Hayes_command_set

As for control from Python - given that the modem would be present as a
serial (COM port) device in the system, I'd have thought that (at worst)
you'd be looking to use PySerial to interact with the modem.  There may also
be more targetted wrappers specifically wrapping modems (don't know, haven't
looked).  And as mentioned before, you can probably also use the more
abstract interface provided by the operating system (TAPI stuff).

And yes, USB is quite different from the POTS (Plain Old Telephone System).
Forget any ideas that they're anywhere the same thing.

Hope that helps.

Walter
-- next part --
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20110123/9e9cfc4a/attach
ment-0001.html>

--

Message: 5
Date: Sun, 23 Jan 2011 17:41:39 -0500
From: bob gailer 
To: David Hutto 
Cc: tutor@python.org
Subject: Re: [Tutor] Telephone app
Message-ID: <4d3caea3.60...@gmail.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 1/23/2011 4:04 PM, David Hutto wrote:

Warning - do NOT connect a telco landline to a USB port. The line
voltage when "on hook" is around 50 V and rises over 100 when ringing.
That will certainly fry the port.

There are expansion cards and other devices designed to connect to the
landline and to a phone. They also process caller id and send touch tone
signals.

I've tried to follow your explanation. It is too vague for me to make
sense of.

I guess you want to take the line that comes to you from your local
telco, stick something computer-wise between it and an ordinary analog
phone, so the computer can receive and process the caller id from an
incoming call, and also ensure that the caller id appears on the phone
itself, and use the computer to dial numbers (NOT known as caller id).

Correct so far?

> It's two fold. First is the obvious of conducting a call/receiving the
> caller id info.

May be obvious to you, but not to me! To support your query please
provide some kind of wiring diagram and define "conducting a call".
> The second is to send directly to the phone, and transmit the caller id
data.

Again this is not very precise or clear. What do you want to send to the
pone?
> By flow through, I mean that the phone has one of 2 states(on the
> hook, off the hook)
I'm OK with that.
> o

Re: [Tutor] Telephone app

2011-01-23 Thread Alan Gauld

"David Hutto"  wrote


By flow through, I mean that the phone has one of 2 states(on the
hook, off the hook), and three sub states(on the hook/off the hook 
in

use/off the hook not in use).


Umm, no. Much more than that.


On the actual phone we pick up the receiver, or press the button on
the cordless to receive, but the line is always connected, meaning 
it

stops at the phone(terminal). I pick up the receiver, and transmit a
series of specific tones which indicate the area code, trunk number
and extension(if I remember this correctly, it's been a while since 
I

studied the phone itself).


You are describing an anlalog phone but with a greatly oversimplified
model. (no dialtone/busy tone/party-line detection etc)
You need to do a lot more research into how analog phones work and
you will need some hardware to interface to it - weither a dedicated
CTI card or a modem.

So I think my main question is what modules might be relevant to 
doing

this? And should I be thinking of it any differently than a USB port
which has 4 pins two data(+-), and two dc current(+-)?


USB is a digital serial bus, phones are analog. They are not at all
alike (quite apart from the voltage issues Bob mentioned).

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Telephone app

2011-01-23 Thread Alan Gauld


"Steven D'Aprano"  wrote

Folks, this question has nothing to do with Python and is off-topic 
for this list. Can you all take it off-list please?


To be fair David is intending doing this with Python.
However he is so far off-base with how he thinks it would work
that he probably does need to go research the technology
options a bit more before coming back and discussing his
preferred options.

There are probably communities on the Internet or Usenet that are 
interested in low-level telecommunications protocols and devices.


Indeed and they would be a good starying point for adcvvice on
how to proceed. Once a techniocal solutoon uis selected we
can get back to how to build it in Python.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Telephone app

2011-01-23 Thread Steven D'Aprano

bob gailer wrote:

On 1/23/2011 4:04 PM, David Hutto wrote:

[...]
I guess you want to take the line that comes to you from your local 
telco, stick something computer-wise between it and an ordinary analog 
phone, so the computer can receive and process the caller id from an 
incoming call, and also ensure that the caller id appears on the phone 
itself, and use the computer to dial numbers (NOT known as caller id).


Folks, this question has nothing to do with Python and is off-topic for 
this list. Can you all take it off-list please?


There are probably communities on the Internet or Usenet that are 
interested in low-level telecommunications protocols and devices. We 
don't go there to talk about Python, please don't stay here talking 
about their areas of expertise.



Thank you.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Telephone app

2011-01-23 Thread bob gailer

On 1/23/2011 4:04 PM, David Hutto wrote:

Warning - do NOT connect a telco landline to a USB port. The line 
voltage when "on hook" is around 50 V and rises over 100 when ringing. 
That will certainly fry the port.


There are expansion cards and other devices designed to connect to the 
landline and to a phone. They also process caller id and send touch tone 
signals.


I've tried to follow your explanation. It is too vague for me to make 
sense of.


I guess you want to take the line that comes to you from your local 
telco, stick something computer-wise between it and an ordinary analog 
phone, so the computer can receive and process the caller id from an 
incoming call, and also ensure that the caller id appears on the phone 
itself, and use the computer to dial numbers (NOT known as caller id).


Correct so far?


It's two fold. First is the obvious of conducting a call/receiving the
caller id info.


May be obvious to you, but not to me! To support your query please 
provide some kind of wiring diagram and define "conducting a call".

The second is to send directly to the phone, and transmit the caller id data.


Again this is not very precise or clear. What do you want to send to the 
pone?

By flow through, I mean that the phone has one of 2 states(on the
hook, off the hook)

I'm OK with that.

off the hook in use/off the hook not in use).


That is not clear.

On the actual phone we pick up the receiver, or press the button on
the cordless to receive, but the line is always connected, meaning it
stops at the phone(terminal). I pick up the receiver, and transmit a
series of specific tones which indicate the area code, trunk number
and extension(if I remember this correctly, it's been a while since I
studied the phone itself).

So I have to receive the signal that the phone is ringing(then I'm
assuming it sends the caller id info in between rings in some form)/or
transmit a series of tones to them to connect.


Huh?

So I think my main question is what modules might be relevant to doing
this?


As someone mentioned earlier - TAPI is your friend.

And should I be thinking of it any differently than a USB port
which has 4 pins two data(+-), and two dc current(+-)?


As I warned above, YES.


--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Telephone app

2011-01-23 Thread Walter Prins
On 23 January 2011 21:04, David Hutto  wrote:

> So I have to receive the signal that the phone is ringing(then I'm
> assuming it sends the caller id info in between rings in some form)/or
> transmit a series of tones to them to connect.
>
> So I think my main question is what modules might be relevant to doing
> this? And should I be thinking of it any differently than a USB port
> which has 4 pins two data(+-), and two dc current(+-)?
>
>
I think you're thinking too low level, as alluded to by Alan this type of
stuff is done via a voice-modem that you can directly control (via serial
port) and get signals from e.g. using its command set.  A common standard
for about 3 decades has been the Hayes command set:
http://en.wikipedia.org/wiki/Hayes_command_set

As for control from Python - given that the modem would be present as a
serial (COM port) device in the system, I'd have thought that (at worst)
you'd be looking to use PySerial to interact with the modem.  There may also
be more targetted wrappers specifically wrapping modems (don't know, haven't
looked).  And as mentioned before, you can probably also use the more
abstract interface provided by the operating system (TAPI stuff).

And yes, USB is quite different from the POTS (Plain Old Telephone System).
Forget any ideas that they're anywhere the same thing.

Hope that helps.

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Telephone app

2011-01-23 Thread David Hutto
> Can you step back a bit and explain what it is you are trying to
> accomplish? "flow through" and "functional flow through" are meaningless
> terms in telecomms - at least so far as I am aware (after my 35 years in
> telecomms engineering...)


It's two fold. First is the obvious of conducting a call/receiving the
caller id info. The second is to send directly to the phone, and
transmit the caller id data.

By flow through, I mean that the phone has one of 2 states(on the
hook, off the hook), and three sub states(on the hook/off the hook in
use/off the hook not in use).

On the actual phone we pick up the receiver, or press the button on
the cordless to receive, but the line is always connected, meaning it
stops at the phone(terminal). I pick up the receiver, and transmit a
series of specific tones which indicate the area code, trunk number
and extension(if I remember this correctly, it's been a while since I
studied the phone itself).

So I have to receive the signal that the phone is ringing(then I'm
assuming it sends the caller id info in between rings in some form)/or
transmit a series of tones to them to connect.

So I think my main question is what modules might be relevant to doing
this? And should I be thinking of it any differently than a USB port
which has 4 pins two data(+-), and two dc current(+-)?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PDF to TXT

2011-01-23 Thread Robert Berman
Hi,

I am trying to convert .pdf files to .txt files. The script I am using
below is mostly taken from research done on Google and it appears to be
the one outline most consistently favored
(http://code.activestate.com/recipes/577095-convert-pdf-to-plain-text/).

I am using Win 7, Python 2.7.1.
My code:

#pdf2txt.py
import sys
import pyPdf
import os

def getPDFContent(path):
content = ""
# Load PDF into pyPDF
pdf = pyPdf.PdfFileReader(file(path, "rb"))
# Iterate pages
for i in range(0, pdf.getNumPages()):
# Extract text from page and add to content
content += pdf.getPage(i).extractText() + " \n"
# Collapse whitespace
# content = u" ".join(content.replace(u"\xa0", u" ").strip().split())
return content

def main():
pdf = sys.argv[1]
filedir,filename = os.path.split(pdf)
nameonly = os.path.splitext(filename)
newname = nameonly[0] + ".txt"
outtxt = os.path.join(filedir,newname)
f = open(outtxt,'w')
f.write(getPDFContent(pdf))
f.close()

main()
exit()

==

The program runs for a while and then dies while in one of the pypdf
functions.  The trace is below. Any insight into how to resolve this
situation will be most appreciated.

Thank you,

Robert

===
The trace I get is:
decimal.InvalidOperation: Invalid literal for Decimal: '.'
File "C:\Users\bermanrl\Projects\ScriptSearch\testdir\pdf2txt.py", line
28, in 
main()
File "C:\Users\bermanrl\Projects\ScriptSearch\testdir\pdf2txt.py", line
25, in main
f.write(getPDFContent(pdf))
File "C:\Users\bermanrl\Projects\ScriptSearch\testdir\pdf2txt.py", line
13, in getPDFContent
content += pdf.getPage(i).extractText() + " \n"
File "C:\Python27\Lib\site-packages\pyPdf-1.13-py2.7-win32.egg\pyPdf
\pdf.py", line 1381, in extractText
content = ContentStream(content, self.pdf)
File "C:\Python27\Lib\site-packages\pyPdf-1.13-py2.7-win32.egg\pyPdf
\pdf.py", line 1464, in __init__
self.__parseContentStream(stream)
File "C:\Python27\Lib\site-packages\pyPdf-1.13-py2.7-win32.egg\pyPdf
\pdf.py", line 1503, in __parseContentStream
operands.append(readObject(stream, None))
File "C:\Python27\Lib\site-packages\pyPdf-1.13-py2.7-win32.egg\pyPdf
\generic.py", line 87, in readObject
return NumberObject.readFromStream(stream)
File "C:\Python27\Lib\site-packages\pyPdf-1.13-py2.7-win32.egg\pyPdf
\generic.py", line 234, in readFromStream
return FloatObject(name)
File "C:\Python27\Lib\site-packages\pyPdf-1.13-py2.7-win32.egg\pyPdf
\generic.py", line 207, in __new__
return decimal.Decimal.__new__(cls, str(value), context)
File "C:\Python27\Lib\decimal.py", line 548, in __new__
"Invalid literal for Decimal: %r" % value)
File "C:\Python27\Lib\decimal.py", line 3844, in _raise_error
raise error(explanation)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a semantic error?

2011-01-23 Thread Richard D. Moores
Thanks, Tutors, for the excellent replies. I think I've got it now.

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] multi-threaded/parallel processing - local tutor

2011-01-23 Thread bruce
not looking for docs.. already have code.

looking to actually talk to someone in the san fran/bay area for an in
person talk/tutor session.

thanks


2011/1/22 शंतनू :
> You may find following useful.
>
> 2.6+ --- http://docs.python.org/library/multiprocessing.html
> 3.x --- http://docs.python.org/dev/library/multiprocessing.html
>
>
> On 23-Jan-2011, at 11:46 AM, bruce wrote:
>
>> Hi.
>>
>> I'm working on a project that uses python to spawn/create multiple
>> threads, to run parallel processes to fetch data from websites. I'm
>> looking to (if possible) go over this in person with someone in the
>> San Fran area. Lunch/beer/oj can be on me!!
>>
>> It's a little too complex to try to describe here, and pasting the
>> code/apps wouldn't do any good without an associated conversation.
>>
>> So, if you're in the Bay area, and you're up to some in person
>> tutoring, let me know.
>>
>> Thanks guys for this list!!
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a semantic error?

2011-01-23 Thread Alan Gauld

"Steven D'Aprano"  wrote

are, more or less, analogous. In principle you could design a 
compiler to try guessing what you probably meant when faced with 
syntax errors:


And in fact there are several such compilers for languages like C.
The Digital Equipment VAX VMS had an IDE with such a compiler
called LSE (Language Sensitive Environment) which could detect
and correct many common C syntax errors such as missing semi-colons
or using = instead of == etc. It was of course not perfect because 
there

are many such xcases where its imp[ossibler to be certain if its a
mistake or a deliberate but unusual construct. But it got it right 9 
times

out of 10...

Languages like ADA and Pascal with much tighter syntax rules
are even easier to correct. Open languages like Lisp and Forth
are more limited in their opportunities.

The LSE tool could be set to automatically correct or present a list
of potential corrections which the user then stepped through at the
end selecting the corrections required. LSE was quite expensive
(about $30,000 for a server license I think, compared to $2000 for
the vanilla C compiler) but it saved a lot of time on our project.

Automatically detecting/correcting semantic errors is sadly not
possible. (I have seen one compiler - for CORAL I think it was?)
which attempted to do so, but it could only spit out a list of
possible errors with what it thought were the probability of error.
And it was wrong at least as often as it was right...

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a semantic error?

2011-01-23 Thread Steven D'Aprano

Richard D. Moores wrote:

But could someone give me a clearcut example of a semantic error? And
a definition that delineates semantic errors from syntax errors.


"Semantics" relates to the *meaning* of words, sentences or programs. In 
common English, we might say this sentence has a few grammatical errors, 
but the semantics are clear:


"I getted the milk out off the fridge and putted them into me coffee."

On the other hand, these sentences are grammatically fine but 
semantically ambiguous:


"Children make nutritious snacks."
"The thief was sentenced to six months in the violin case."
"Cocaine users are turning to ice."
"Police shoot man with crossbow."
"The building workers are refusing to work after fatal accidents."

and of course the classic example of a grammatically valid sentence with 
no semantic meaning:


"Colourless green ideas sleep furiously."

Often you can guess the meaning of such ambiguous sentences from domain 
specific knowledge. We know that eating children is generally frowned 
upon, and so we reject the interpretation of the snacks being made 
*from* the children rather than *by* the children.


Other times it is much harder to resolve the ambiguity:

"The English history teacher marked the test paper."

Did she, or he, teach English history, or was she English and a history 
teacher?



Bringing it back to programming in general, and Python specifically, we 
don't talk about "grammar errors" but "syntax errors" instead. The two 
are, more or less, analogous. In principle you could design a compiler 
to try guessing what you probably meant when faced with syntax errors:


x, y = alist[1;3]

probably is a typo of ; instead of :

but such cases are far too rare to be worth worrying about, and in any 
case, "Do what I mean" (DWIM) tools are risky and rarely do what you mean.


http://www.catb.org/~esr/jargon/html/D/DWIM.html

A *semantic* error would be something like this:

x, y = alist[1:2]

when what you actually needed was alist[1:3]. Or:

mylist = mylist.sort()

(did you really want mylist to be set to None? I don't think so.)

It's rare that the compiler can tell what you want, as opposed to what 
you asked for, with any accuracy. How could the compiler tell that when 
you wrote:


x = math.tan(1.25)

you actually wanted math.atan instead? Or should that be 
math.tan(1.25*pi)? Or something else?




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a semantic error?

2011-01-23 Thread Wayne Werner
On Sun, Jan 23, 2011 at 6:09 AM, Richard D. Moores wrote:

> Is a semantic error one that Python doesn't see as an error -- no
> error is raised; whereas syntax errors aren't errors unless Python
> sees them as "Syntax Error"s?
>
>
Yes:
http://en.wikipedia.org/wiki/Semantic_error

Here is a semantic error:

total = 0
for x in xrange(1,11):
 total = x   # Sums up x

The actual intent was to use += for total.

Another example

# Sums up numbers 1-10 inclusive
total = 0
for x in xrange(1,10):
  total += x

Of course (x)range doesn't include the end index, so in both cases the
program would compile and execute, but neither would give the programmers
intended result.

HTH
-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a semantic error?

2011-01-23 Thread Hugo Arts
On Sun, Jan 23, 2011 at 1:09 PM, Richard D. Moores  wrote:
>
> Is a semantic error one that Python doesn't see as an error -- no
> error is raised; whereas syntax errors aren't errors unless Python
> sees them as "Syntax Error"s?
>

Pretty much this. A semantic error is also called a logic error. A
program with a semantic error will not crash or terminate abnormally,
but it will not produce the output that you want.

Semantic errors are much harder to catch than syntax errors, because
programs with semantic errors are still valid programs in the
language. A syntax error makes the program invalid, which means the
computer can easily identify it. It's up to you to identify semantic
errors.

HTH,
Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What is a semantic error?

2011-01-23 Thread Richard D. Moores
Here are 3 examples of syntax errors:

>>> print('and)
Traceback (most recent call last):
  File "", line 1, in 
Syntax Error: print('and): , line 112
>>> if 3 > 2
...   print(3)
...
Traceback (most recent call last):
  File "", line 1, in 
Syntax Error: if 3 > 2: , line 19
>>> if 34 345:
...print(34)
...
Traceback (most recent call last):
  File "", line 1, in 
Syntax Error: if 34 345:: , line 19
>>>

But could someone give me a clearcut example of a semantic error? And
a definition that delineates semantic errors from syntax errors.

I googled this one up,
http://ubuntuforums.org/showthread.php?t=527855, where the guy uses
'&' where he should have used 'and'. That's clearcut, but is it really
a semantic error? If he had used, say, '!' where he used '&' (which it
seems has meaning in that context), that would be a syntax error,
right?

>>> if 3 == 3 & 4 > 3:
...   print("Duh")
...
>>>

>>> if 3 == 3 ! 4 > 3:
...print("Doh!")
...
Traceback (most recent call last):
  File "", line 1, in 
Syntax Error: if 3 == 3 ! 4 > 3:: , line 111
>>>

Is a semantic error one that Python doesn't see as an error -- no
error is raised; whereas syntax errors aren't errors unless Python
sees them as "Syntax Error"s?

Thanks,

Dick Moores
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on RE

2011-01-23 Thread Albert-Jan Roskam


http://imgs.xkcd.com/comics/regular_expressions.png

;-)
 
Cheers!!
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the 
Romans ever done for us?
~~





From: Steven D'Aprano 
To: tutor@python.org
Sent: Sun, January 23, 2011 4:10:35 AM
Subject: Re: [Tutor] Help on RE

tee chwee liong wrote:
> thanks for making me understand more on re. re is a confusing topic as i'm 
>starting on python. 
>

I quote the great Jamie Zawinski, a world-class programmer and hacker:

Some people, when confronted with a problem, think 'I know, I'll
use regular expressions." Now they have two problems.


Zawinski doesn't mean that you should never use regexes. But they should be 
used 
only when necessary, for problems that are difficult enough to require a 
dedicated domain-specific language for solving search problems.

Because that's what regexes are: they're a programming language for text 
searching. They're not a full-featured programming language like Python 
(technically, they are not Turing Complete) but nevertheless they are a 
programming language. A programming language with a complicated, obscure, 
hideously ugly syntax (and people complain about Forth!). Even the creator of 
Perl, Larry Wall, has complained about regex syntax and gives 19 serious faults 
with regular expressions:

http://dev.perl.org/perl6/doc/design/apo/A05.html

Most people turn to regexes much too quickly, using them to solve problems that 
are either too small to need regexes, or too large. Using regexes for solving 
your problem is like using a chainsaw for peeling an orange.

Your data is very simple, and doesn't need regexes. It looks like this:


Platform: PC
Tempt : 25
TAP0 :0
TAP1 :1
+
Port Chnl Lane EyVt EyHt
+
0  1  1  75  55
0  1  2  10 35
0  1  3  25 35
0  1  4  35 25
0  1  5  10 -1
+
Time: 20s


The part you care about is the table of numbers, each line looks like this:

0  1  5  10 -1

The easiest way to parse this line is this:

numbers = [int(word) for word in line.split()]

All you need then is a way of telling whether you have a line in the table, or 
a 
header. That's easy -- just catch the exception and ignore it.

template = "Port=%d, Channel=%d, Lane=%d, EyVT=%d, EyHT=%d"
for line in lines:
try:
numbers = [int(word) for word in line.split()]
except ValueError:
continue
print(template % tuple(numbers))


Too easy. Adding regexes just makes it slow, fragile, and difficult.


My advice is, any time you think you might need regexes, you probably don't.


-- Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on RE

2011-01-23 Thread Japhy Bartlett
it's a bug in your regex - you want something like "-?\d+"

- japhy

On Sat, Jan 22, 2011 at 7:38 PM, tee chwee liong  wrote:
> hi,
>
> i have a set of data and using re to extract it into array. however i only
> get positive value, how to extract the whole value including the -ve sign?
> For eg:
>
> Platform: PC
> Tempt : 25
> TAP0 :0
> TAP1 :1
> +
> Port Chnl Lane EyVt EyHt
> +
> 0  1  1  75  55
> 0  1  2  10 35
> 0  1  3  25 35
> 0  1  4  35 25
> 0  1  5  10 -1
> +
> Time: 20s
>
> When i run my code, i get 1 instead of -1 in the last line. here is my code.
> pls advise. i'm using Python 2.5 and Win XP. tq
> ##code###
> import re
> file = open("C:/Python25/myscript/plot/sampledata.txt", "r")
> x1 = []
> y1 = []
> y2 = []
> for line in file:
>     numbers = re.findall("\d+", line)
>     print numbers
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor