[Tutor] Starting to write a scanf-like module for Python
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Mon Aug 16 04:46:05 CEST 2004
On Sun, 15 Aug 2004, Alan Gauld wrote:
> > I thought it might make a nice weekend project to write scanf for
> > Python; it's nowhere near done yet, but it's sorta working... *grin*
> >
> > http://hkn.eecs.berkeley.edu/~dyoo/python/scanf.py
> >
> > I want to gauge some input from folks on the list to see if this might
> > be useful for folks.
>
> If it were sscanf() it would be really useful.
[some text cut]
> I would seriously consider sscanf() as a better project - and easier -
> and leave the acquiring the input string to raw_input or file.read()
Hi Alan,
Too late: I had already implemented it. *grin*
In fact, I use it for my unit test cases. Here's a sample of what I test:
###
def ScanfTests(unittest.TestCase):
def testIntegerScanning(self):
self.assertEquals((42, 43),
sscanf(" 42\n 43 ", "%d %d"))
def testWordScanning(self):
self.assertEquals(("hello", "world"),
sscanf(" hello world", "%s %s"))
def testSuppression(self):
self.assertEquals((), sscanf(" hello world", "%*s %*s"))
###
I do know I need to document the module better, so I'll add some more
documentation strings. What this 'scanf' module provides are the
following functions:
scanf(formatString) -- reads from standard input
sscanf(inputString, formatString) -- reads from string input
fscanf(inputFile, formatString) -- reads from a file-like object
I've also mimicked the 're' module sligthly in the sense that the module
allows one to "compile" a format string, and then use the compiled pattern
over and over:
###
>>> import scanf
>>> patternReader = scanf.compile(" (%d, %d) ")
>>> patternReader
<function f at 0x850b0>
>>>
>>>
>>> from StringIO import StringIO
>>> sampleFile = StringIO('''
... (3, 3)
... (17, 42)
... (-1, 5)
... ''')
>>> sampleBuffer = scanf.CharacterBufferFromFile(sampleFile)
>>> patternReader(sampleBuffer)
(3, 3)
>>> patternReader(sampleBuffer)
(17, 42)
>>> patternReader(sampleBuffer)
(-1, 5)
>>> patternReader(sampleBuffer)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "scanf.py", line 310, in f
raise IncompleteCaptureError, (e, tuple(results))
scanf.IncompleteCaptureError: (<scanf.FormatError instance at 0x84580>,
())
###
Hmmm... I guess I should simplify the usage here so that the user doesn't
have to manually call the internal "CharacterBufferFromFile" thingy.
> scanf() has so many bugs/features nobody in their right mind uses it do
> they?
Yes, that's the problem. So I'm not quite sure what features people
really find useful in scanf(). *grin*
But I personally need something that does integer and word scanning, so
that's what I've implemented so far. I don't mind if this does only a
subset of C's scanf(), just as long as it does the most important stuff,
the stuff that people actually use with the *scanf() C functions.
More information about the Tutor
mailing list