[Tutor] IOError with fcntl.ioctl

2005-08-16 Thread Richi
Hi,

I'm trying to get the number of bytes available on an open serial device 
(on Linux). I try the following commands:

 >>> import os
 >>> import fcntl
 >>> import termios
 >>> fd = os.open ("/dev/ttyS0", os.O_RDWR)
 >>> fcntl.ioctl (fd, termios.TIOCINQ)

and after the last line I get the following error:

Traceback (most recent call last):
  File "", line 1, in ?
IOError: [Errno 14] Bad address

I've tried fcntl.ioctl (fd, termios.FIONREAD) (which is supposed to do 
the same thing, I think), and I still get the previous error. In fact, 
any ioctl() command I give to that file descriptor gives that result.

I'd appreciate any help in getting this to work.
--

Richi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] LiveWires problems

2005-08-16 Thread Luis N
On 8/15/05, ZIYAD A. M. AL-BATLY <[EMAIL PROTECTED]> wrote:
> On Mon, 2005-08-15 at 11:52 -0400, Michael Murphy wrote:
> > Hi all
> >
> > I'm having problems with installing LiveWire for python for Linux
> > (Linspire 5.0 to be exact) and I'm having trouble compiling setup.py.
> > Heres the results:
> >
> > running install
> > running build
> > running build_py
> > running install_lib
> > creating /usr/lib/python2.3/site-packages/livewires
> > error: could not create '/usr/lib/python2.3/site-packages/livewires':
> > Permission denied
> >
> > I'm new at this but I'm sure this is an easy fix but I can't figure it
> > out, any help is appreciated.

Consider typing:

python setup.py install home=$HOME 

instead of,

python setup.py install

You will then install into ~/lib/python2.3/livewires if this location
is acceptable to livewires.

Luis.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] deriving class from file to handle input line numbers?

2005-08-16 Thread Duncan Gibson

I was sure that this must be a frequently asked [homework?] question,
but trying to search for 'file open line number' didn't throw up the
sort of answers I was looking for, so here goes...

I regularly write parsers for simple input files, and I need to give
helpful error messages if the input is invalid. The three most helpful
pieces on information are file name, line number and line content. Of
course I can use a global variable to track how many times f.readline()
is called, but I was wondering whether there is a more OO or Python
way of encapsulating this within a class derived from file.

What I have below is the minimal interface that I could come up with,
but it is a file adaptor rather than a derived class, and it doesn't
seem quite clean to me, because I have to open the file externally
and then pass the file object into the constructor.

Is there a better way of doing it, or am I chasing rainbows?

Cheers
Duncan

class LineCountedInputFile(object):
"""
add input line count to minimal input File interface

The file must be opened externally, and then passed into the
constructor.  All access should occur through the readLine method,
and not using normal File methods on the external file variable,
otherwise things will get out of sync and strange things could
happen, including incorrect line number.
"""

__slots__ = (
'_inputFile',
'_lineNumber')

def __init__(self, inputFile):
"""
create a LineCountedInputFile adaptor object

:param inputFile: existing File object already open for reading only
:type  inputFile: `File`
"""

assert isinstance(inputFile, file)
assert not inputFile.closed and inputFile.mode == 'r'

self._inputFile = inputFile
self._lineNumber = 0

#--

def readLine(self):
"""
call file.readline(), strip excess whitespace, increment line number

:return: next line of text from file minus excess whitespace, or
None at end-of-file
:rtype:  str
"""

line = self._inputFile.readline()
if len(line) == 0:
return None
self._lineNumber += 1
return line.strip()

#--

def _get_fileName(self):
return self._inputFile.name

fileName = property(_get_fileName, None, None, """(read-only)""")

#--

def _get_lineNumber(self):
return self._lineNumber

lineNumber = property(_get_lineNumber, None, None, """(read-only)""")

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Silly loop question

2005-08-16 Thread mailing list
Hi all, 

I should know this, but it seems to elude me at present. 

I have a loop which is looping through a line of chars by index. 

So, one of these -  

for i in range(len(line)):


Now, question is, is there a simple way to 'fast forward'  index i?

At the moment, I'm doing it like this - 

changeIndex = None
al = len(line)
for i in range(al):

 if changeIndex and (i < changeIndex):
 continue

if line[i] == "{":
nextRightBracket = line.find("}",i)
#other stuff happens
changeIndex = nextRightBracket + 1


As I said, pretty dumb question, so somewhat expecting to be told to
read Alan's tutorial again. *grin*


Regards, 


Liam Clarke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] deriving class from file to handle input line numbers?

2005-08-16 Thread Kent Johnson
Duncan Gibson wrote:
> I was sure that this must be a frequently asked [homework?] question,
> but trying to search for 'file open line number' didn't throw up the
> sort of answers I was looking for, so here goes...
> 
> I regularly write parsers for simple input files, and I need to give
> helpful error messages if the input is invalid. The three most helpful
> pieces on information are file name, line number and line content. Of
> course I can use a global variable to track how many times f.readline()
> is called, but I was wondering whether there is a more OO or Python
> way of encapsulating this within a class derived from file.

If you just want to keep track of line numbers as you read the file by lines, 
you could use enumerate():

f = open('myfile.txt')
for line_number, line in enumerate(f):
  ...

For any iterable object, enumerate() returns a sequence of (index, value) for 
each value in the object.

Your class has the advantage of maintaining the line number internally and 
customizing readline() to your needs.

More comments inline.

Kent

> What I have below is the minimal interface that I could come up with,
> but it is a file adaptor rather than a derived class, and it doesn't
> seem quite clean to me, because I have to open the file externally
> and then pass the file object into the constructor.

Why? You should be able to open the file in the constuctor and provide close() 
and __del__() methods to close the file. What problem did you have when 
deriving from file? 

> class LineCountedInputFile(object):
> """
> add input line count to minimal input File interface
> 
> The file must be opened externally, and then passed into the
> constructor.  All access should occur through the readLine method,
> and not using normal File methods on the external file variable,
> otherwise things will get out of sync and strange things could
> happen, including incorrect line number.
> """
> 
> __slots__ = (
> '_inputFile',
> '_lineNumber')

__slots__ is overkill, it is intended as a way to save memory in classes that 
will have many instances, and discouraged otherwise.

> 
> def __init__(self, inputFile):
> """
> create a LineCountedInputFile adaptor object
> 
> :param inputFile: existing File object already open for reading only
> :type  inputFile: `File`
> """
> 
> assert isinstance(inputFile, file)
> assert not inputFile.closed and inputFile.mode == 'r'
> 
> self._inputFile = inputFile
> self._lineNumber = 0
> 
> #--
> 
> def readLine(self):

I would call this method readline() to match what file does.

> """
> call file.readline(), strip excess whitespace, increment line number
> 
> :return: next line of text from file minus excess whitespace, or
> None at end-of-file
> :rtype:  str
> """
> 
> line = self._inputFile.readline()
> if len(line) == 0:
> return None
> self._lineNumber += 1
> return line.strip()
> 
> #--
> 
> def _get_fileName(self):
> return self._inputFile.name
> 
> fileName = property(_get_fileName, None, None, """(read-only)""")

Maybe just call this property 'name' to match what file does.
 
> #--
> 
> def _get_lineNumber(self):
> return self._lineNumber
> 
> lineNumber = property(_get_lineNumber, None, None, """(read-only)""")

The use of properties to make read-only attributes might be overkill also. I 
would just have a lineNumber attribute but that is more of a judgement call.

> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] convert a file from plaintext(Ascii) to unicode? very quickquestions

2005-08-16 Thread Michael Lange
On Mon, 15 Aug 2005 22:51:20 -0400
Kent Johnson <[EMAIL PROTECTED]> wrote:

> I think Luke's suggestion will work if you use f.read() (to read the whole 
> file as a single string) instead of f.readlines() and f.write() instead of 
> writelines().
> 
> Kent
> 

And if you want to convert ascii into unicode you need to call * decode() * ( 
which does pretty much the same as unicode() )
on the string, not encode() .

Michael

> luke wrote:
> > List:
> > I'm forwarding this private message(hope you don't mind Denise)
> > I personally have no idea what to do, but
> > someone else might be able to help.
> > -Luke
> > 
> > 
> > - Forwarded Message -
> > 
> > text is a list, so you can't encode it.  but you can iterate over each
> > of the elements and encode them.  I have tried several variations of
> > that, but keep ending up with all my newlines being little boxes. any
> > ideas?
> > 
> > Thanks,
> > Denise
> > 
> > On 8/15/05, luke <[EMAIL PROTECTED]> wrote:
> >>I dont know much about Unicode but it seems like
> >>f = file(filename, "r")
> >>text = f.readlines()
> >>text = text.encode()
> >>#or maybe just text.encode()?
> >>f.close()
> >>
> >>should encode the filetext to unicode.
> >>then you could do a
> >>f = file(filename, "w")
> >>f.writelines(text)
> >>f.close()
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Silly loop question

2005-08-16 Thread Kent Johnson
mailing list wrote:
> Hi all, 
> 
> I should know this, but it seems to elude me at present. 
> 
> I have a loop which is looping through a line of chars by index. 
> 
> So, one of these -  
> 
> for i in range(len(line)):
> 
> 
> Now, question is, is there a simple way to 'fast forward'  index i?
> 
> At the moment, I'm doing it like this - 
> 
> changeIndex = None
> al = len(line)
> for i in range(al):
> 
>  if changeIndex and (i < changeIndex):
>  continue
> 
> if line[i] == "{":
>   nextRightBracket = line.find("}",i)
> #other stuff happens
> changeIndex = nextRightBracket + 1

I would do this with a while loop and an explicit index counter:

al = len(line)
i = 0
while i < al:
if line[i] == "{":
nextRightBracket = line.find("}",i)
#other stuff happens
i = nextRightBracket + 1
else:
i += 1

You could also make a custom iterator that you could set but it doesn't seem 
worth the trouble. If you just had to skip one or two values I would make an 
explicit iterator and call next() on it, but to advance an arbitrary amount 
that is awkward.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] LiveWires problems

2005-08-16 Thread Michael Murphy
Compiling it in Root worked, thanks.

On 8/16/05, Luis N <[EMAIL PROTECTED]> wrote:
> On 8/15/05, ZIYAD A. M. AL-BATLY <[EMAIL PROTECTED]> wrote:
> > On Mon, 2005-08-15 at 11:52 -0400, Michael Murphy wrote:
> > > Hi all
> > >
> > > I'm having problems with installing LiveWire for python for Linux
> > > (Linspire 5.0 to be exact) and I'm having trouble compiling setup.py.
> > > Heres the results:
> > >
> > > running install
> > > running build
> > > running build_py
> > > running install_lib
> > > creating /usr/lib/python2.3/site-packages/livewires
> > > error: could not create '/usr/lib/python2.3/site-packages/livewires':
> > > Permission denied
> > >
> > > I'm new at this but I'm sure this is an easy fix but I can't figure it
> > > out, any help is appreciated.
> 
> Consider typing:
> 
> python setup.py install home=$HOME
> 
> instead of,
> 
> python setup.py install
> 
> You will then install into ~/lib/python2.3/livewires if this location
> is acceptable to livewires.
> 
> Luis.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 

Mike

Erie Freeze will rise again!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regexp with multiple patterns in Python

2005-08-16 Thread Kent Johnson
Kristian Evensen wrote:
> What I want to do is to check for two patterns to make sure all 
> occurrences of pattern1 and pattern2 come in the same order as they do 
> in the file I parse. It it contains a number of computer-games I would 
> like the output to look something like this:
> 
> PC, Battlefield, Battlefield2
> 
> PS2, Battlefield 2: Modern Combat.
> 
>  
> 
> The file is constructed somewhat similar to this:
> 
> PC
> Battlefield, Battfiled2
> PS2
> Battlefield 2: Modern Combat

Are you trying to *check* that the data is in this format, or are you trying to 
read this format and output a different format?

If the data is really this simple you can just iterate over the lines in pairs:
f = open('myfile.txt')
fi = iter(f)
for line in fi:
  nextline = fi.next()
  print '%s, %s' % (line.strip(), nextline.strip())

You could add code to the loop to check that the data is in the expected format.

> Using the following expression (and re.findall) I get somewhat closer:
> 
> pattern8 = re.compile(r'search.asp\?title=battlefield.*?><.*?>(PCCD|XBOX 
> 360|XBOX|PLAYSTATION PSP|PLAYSTATION 2) - 
> TITLE<|game.asp\?id=(\d+).*?><.*?><.*?>(.*?)<')

>From this regex it looks like the data is actually in XML. You might want to 
>use an XML parser like ElementTree or BeautifulSoup to parse the file, then 
>extract the data from the resulting tree.

If neither of these suggestions helps, please post a sample of your actual 
data, the actual results you want, and the code you have so far.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] deriving class from file to handle input line numbers?

2005-08-16 Thread Duncan Gibson
Kent Johnson wrote:
> If you just want to keep track of line numbers as you read the file by lines, 
> you could use enumerate():
> 
> f = open('myfile.txt')
> for line_number, line in enumerate(f):
>   ...

This is neat, but not all of the parsers work on a 'line by line' basis,
so sometimes there are additional calls to f.readline() or equivalent
in other places in the code based on what has just been read.

> What problem did you have when deriving from file?

To be perfectly honest, I didn't try it because even if I had declared

class MyFile(file):
etc

I couldn't see how to have an instance of MyFile returned from the
built-in 'open' function. I thought this was the crux of the problem.

So what I have done is provide a different interface completely,

class MyFile(object):
etc

I could extend this to take the file name in the constructor, and
add a MyFile.open() method, but then I can no longer substitute
any MyFile instances in places that expect 'file' instances.

Now that I've started to explain all of this to someone else, I'm
starting to wonder whether it really matters. Do I really need it
to be substitutable for 'file' after all ?

I need to sit in a darkened room and think for a while...

Cheers
Duncan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] deriving class from file to handle input line numbers?

2005-08-16 Thread Kent Johnson
Duncan Gibson wrote:
> Kent Johnson wrote:
> To be perfectly honest, I didn't try it because even if I had declared
> 
> class MyFile(file):
> etc
> 
> I couldn't see how to have an instance of MyFile returned from the
> built-in 'open' function. I thought this was the crux of the problem.

open() is actually just an alias for file():
 >>> open is file
True

But if you have your own file class then you will call it's constructor rather 
than open(). Here is a simple example:

 >>> class MyFile(file):
 ...   def __init__(self, *args, **kwds):
 ... self.lineNumber = 0
 ... super(MyFile, self).__init__(*args, **kwds)
 ...   def readline(self):
 ... self.lineNumber += 1
 ... return super(MyFile, self).readline()
 ...
 >>> f=MyFile('build.xml')
 >>> f.lineNumber
0
 >>> f.readline()
'\n'
 >>> f.lineNumber
1
 >>> f.readline()
'

Re: [Tutor] deriving class from file to handle input line numbers?

2005-08-16 Thread Duncan Gibson

I wrote:
> > class MyFile(file):
> > etc
> > 
> > I couldn't see how to have an instance of MyFile returned from the
> > built-in 'open' function. I thought this was the crux of the problem.

Kent Johnson replied:
> open() is actually just an alias for file():
>  >>> open is file
> True

Thank you very much! You have just provided me with the vital piece of
information I needed and everything has just clicked into place.

Now that I know that I've searched the documentation again and found:

The file() constructor is new in Python 2.2 and is an alias for
open(). Both spellings are equivalent. The intent is for open()
to continue to be preferred for use as a factory function which
returns a new file object. The spelling, file is more suited to
type testing (for example, writing "isinstance(f, file)").

See http://docs.python.org/lib/built-in-funcs.html#l2h-25

Cheers
Duncan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Silly loop question

2005-08-16 Thread Alan G
> changeIndex = None
> al = len(line)
> for i in range(al):

And theres the problem.
Pythonprovides two loops, for is really a *foreach* so if you don't
want to process *each* item - eg by jumping forward - then you really
should use while and manage the index thataway.

You can hack about with the indexin a for but basically its abusing
the intent of a for loop. Beter to explicitly hack about in a while 
loop!

So:

while i < len(line):
   if changeIndex and i < changeIndex
  i = changeIndex

   if line[i] == "{":
  nextRightBracket = line.find("}",i)
  #other stuff happens
  changeIndex = nextRightBracket + 1
   else:
  i += 1

> read Alan's tutorial again. *grin*

Umm, well the section on branching does refer to the two loops and 
when
each is most appropriate, so...  :-)

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] convert a file from plaintext(Ascii) to unicode? very quickquestions

2005-08-16 Thread D. Hartley
Thanks, everyone!

On 8/16/05, Michael Lange <[EMAIL PROTECTED]> wrote:
> On Mon, 15 Aug 2005 22:51:20 -0400
> Kent Johnson <[EMAIL PROTECTED]> wrote:
> 
> > I think Luke's suggestion will work if you use f.read() (to read the whole 
> > file as a single string) instead of f.readlines() and f.write() instead of 
> > writelines().
> >
> > Kent
> >
> 
> And if you want to convert ascii into unicode you need to call * decode() * ( 
> which does pretty much the same as unicode() )
> on the string, not encode() .
> 
> Michael
> 
> > luke wrote:
> > > List:
> > > I'm forwarding this private message(hope you don't mind Denise)
> > > I personally have no idea what to do, but
> > > someone else might be able to help.
> > > -Luke
> > >
> > >
> > > - Forwarded Message -
> > >
> > > text is a list, so you can't encode it.  but you can iterate over each
> > > of the elements and encode them.  I have tried several variations of
> > > that, but keep ending up with all my newlines being little boxes. any
> > > ideas?
> > >
> > > Thanks,
> > > Denise
> > >
> > > On 8/15/05, luke <[EMAIL PROTECTED]> wrote:
> > >>I dont know much about Unicode but it seems like
> > >>f = file(filename, "r")
> > >>text = f.readlines()
> > >>text = text.encode()
> > >>#or maybe just text.encode()?
> > >>f.close()
> > >>
> > >>should encode the filetext to unicode.
> > >>then you could do a
> > >>f = file(filename, "w")
> > >>f.writelines(text)
> > >>f.close()
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Very simple question

2005-08-16 Thread Jack Hartland
Hello
i am just starting out with python and have already hit a problem!
howdo you create a new line!  i have tryed tab etc and enter runs the
command. Please help


-- 
Many Thanks 
Jack Hartland
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Very simple question

2005-08-16 Thread Kent Johnson
Jack Hartland wrote:
> Hello
> i am just starting out with python and have already hit a problem!
> howdo you create a new line!  i have tryed tab etc and enter runs the
> command. Please help

Take a look at Danny Yoo's IDLE tutorial:
http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IOError with fcntl.ioctl

2005-08-16 Thread Richi
Hi, All.

Crave pardon if that question of mine was too basic to merit a reply, 
but I couldn't find anything by searching. Newbie Python user here. I 
finally did find the answer and thought I'd post it in case someone 
might come along with the same question.

The problem is with the way I was using fcntl.ioctl(). Actually, the 
problem's even more fundamental than that. ioctl(2) is called the Swiss 
Army Knife of I/O programming on Unix environments. That makes it hard 
to abstract. Since it's uses is varied and complex, it's become more 
difficult to escalate that abstraction to a higher layer (such as Python).

What I found in the source code for Python is that the programmer's made 
a good attempt at keeping ioctl(2)'s abstraction, but not completely. 
High-level language programmers will be forced to switch to low-level 
when doing IO programming with Python on *nix.

It turns out that one needs to pass a 3rd parameter (and possibly a 4th) 
if one expects a result value from calling ioctl. As documented in 
fcntl.ioctl's pydoc, fcntl.ioctl() returns the result of calling ioctl() 
which is just a 0 for success and -1 for error. Passing an immutable 
sequence as the 3rd parameter will modify its behavior so that it 
returns the expected arg value.

I've modified the code so now it looks like:

import os
import termios
import fcntl

fd = os.open ("/dev/ttyS0", os.O_RDWR)
n = fcntl.ioctl (fd, termios.TIOCINQ, "")
y = 0
i = len(n)
while i:
   y <<= 8
   y |= ord (n[i-1])
   i -= 1

# Result in y

Perhaps in the future someone will modify fcntlmodule.c to improve this.

Richi wrote:

> I'm trying to get the number of bytes available on an open serial device 
> (on Linux). I try the following commands:
> 
>  >>> import os
>  >>> import fcntl
>  >>> import termios
>  >>> fd = os.open ("/dev/ttyS0", os.O_RDWR)
>  >>> fcntl.ioctl (fd, termios.TIOCINQ)
> 
> and after the last line I get the following error:
> 
> Traceback (most recent call last):
>   File "", line 1, in ?
> IOError: [Errno 14] Bad address
> 
> I've tried fcntl.ioctl (fd, termios.FIONREAD) (which is supposed to do 
> the same thing, I think), and I still get the previous error. In fact, 
> any ioctl() command I give to that file descriptor gives that result.
> 
> I'd appreciate any help in getting this to work.
--

Richi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor