Newbie completely confused

John Machin sjmachin at lexicon.net
Sat Sep 22 00:07:49 EDT 2007


On Sep 22, 2:34 am, Jeroen Hegeman <jeroen.hege... at gmail.com> wrote:
[snip]
> ...processing all 2 files found
> --> 1/2: ./test_file0.txt
> Now reading ...
> DEBUG readLines A took 0.093 s
> ...took 8.85717201233 seconds

Your code does NOT include any statements that could have produced the
above line of output -- IOW, you have not posted the code that you
actually ran. Your code is already needlessly monstrously large.
That's two strikes against anyone bothering to try to nut out what's
going wrong, if indeed anything is going wrong.

[snip]
>
> The original problem showed up using Python 2.4.3 under linux (Fedora
> Core 1).
> Python 2.3.5 on OS X 10.4.10 (PPC) appears not to show this issue(?).

And Python 2.5.1 does what? Strike 3.

>
> P.S. Any ideas on optimising the input to the classes would be
> welcome too ;-)

1. What is the point of having a do-nothing __init__ method? I'd
suggest making the __init__method do the "input".

2. See below

[snip]
>
> class LongClass:
>
>      def __init__(self):
>          return
>      def clear(self):
>          return
>      def input(self, foo, c):
>          self.item0 = float(foo[c]); c += 1
>          self.item1 = float(foo[c]); c += 1
[multiple snips ahead]
>          self.item18 = float(foo[c]); c+=1
>          self.item19 = int(foo[c]); c+=1
>          self.item20 = float(foo[c]); c+=1
>          self.item27 = bool(int(foo[c])); c+=1
>          self.item30 = (foo[c] == "1"); c += 1
>          self.item31 = (foo[c] == "1"); c += 1
>          self.item47 = bool(int(foo[c])); c+=1
>          return c

at global level:

converters = [float] * 48
cvlist = [
    (int, (19, 22, 26, 34, 40, 46)),
    (lambda z: bool(int(z)), (27, 47)),
    (lambda z: z == "1", (30, 31, 36, 37, 42)),
    ]
for func, indexes in cvlist:
    for x in indexes:
        converters[x] = func
enumerated_converters = list(enumerate(converters))

Then:

         def input(self, foo, c):
             self.item = [func(foo[c+x]) for x, func in
enumerated_converters]
             return c + 48

which requires you refer to obj.item[19] instead of obj.item19

If you *must* use item19 etc, then try this:

for x, func in enumerated_converters:
    setattr(self, "item%d" % x, func(foo[c+x]))

You could also (shock, horror) use meaningful names for the
attributes ... include a list of attribute names in the global stuff,
and put the relevant name in as the 2nd arg of setattr() instead of
itemxx.

For handling the bit extraction stuff, either

(a) conversion functions have a 2nd arg which defaults to None and
whose usage depends on the function itself ... would be mask or bit
position (or could be e.g. a scale factor for implied-decimal-point
input)

or

(b) do a loop over the bit positions

HTH,
John




More information about the Python-list mailing list