Program runs in all directories, except one..

T

Theo v. Werkhoven

Goodday,

Something strange going on here.
A piece of code I wrote bombs out in one of de directories under $HOME,
but not in others.
Here's a snipped:

#v+
def bin2asc(c):
s=''
for i in range(0,8):
if c & 1<<(7-i):
s+='1'
else:
s+='0'
return 'b'+s

def shiftout(numoctets):
os.system('clear')
ser = serial.Serial(0)
from array import array
octarray = [0]
for i in range(0,numoctets-1):
octarray.append(0)

for octet in range(numoctets,0,-1):
octarray[octet-1]=1
for shift in range(0,8):
strg = array("B",octarray).tostring()
for oct in range(len(strg)):
print 'octet%-11s' % oct,
print
for byte in array("B",octarray):
print '%-2s' % bin2asc(int(byte)),
print '%-6s' % hex(int(byte)),
print; print
try:
dummy = raw_input("Druk op een toets om de bytes te verzenden ")
except KeyboardInterrupt, msg:
print; usage("Exiting..")
sys.exit()
ser.write(strg)
octarray[octet-1] = octarray[octet-1] << 1
os.system('clear')
octarray[octet-1]=0
print "done..
#v-

Here's the output from the directory it bombs out in:
#v+
theo:/home/theo/Devel/Python $ python serialLEDtest-cli.py
Hoeveel chips van het type HC595 zitten er op het LED board? 2
0
7865
8162
8220
17307
251430
5886084
5886182
5891527
5892283
5901048
5901062
Traceback (most recent call last):
File "serialLEDtest-cli.py", line 96, in <module>
main()
File "serialLEDtest-cli.py", line 93, in main
shiftout(numoctets)
File "serialLEDtest-cli.py", line 62, in shiftout
strg = array("B",octarray).tostring()
TypeError: 'list' object is not callable
#v-
Where the numbers come from I don't know, python internal?
#v+
theo:/home/theo/Devel/Python$ stat serialLEDtest-cli.py
File: `serialLEDtest-cli.py'
Size: 2641 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 69113 Links: 4
Access: (0644/-rw-r--r--) Uid: ( 1000/ theo) Gid: ( 100/ users)
Access: 2007-04-28 13:28:23.000000000 +0200
Modify: 2007-04-28 13:00:33.000000000 +0200
Change: 2007-04-28 13:22:38.000000000 +0200
#v-

Now i've made hard links to other directories, like $HOME and $HOME/bin
#v+
theo:/home/theo $ stat serialLEDtest-cli.py
File: `serialLEDtest-cli.py'
Size: 2641 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 69113 Links: 4
Access: (0644/-rw-r--r--) Uid: ( 1000/ theo) Gid: ( 100/ users)
Access: 2007-04-28 13:30:02.000000000 +0200
Modify: 2007-04-28 13:00:33.000000000 +0200
Change: 2007-04-28 13:22:38.000000000 +0200
#v-

And when I run (the same script) from that location:
#v+
theo:/home/theo $ python serialLEDtest-cli.py
Hoeveel chips van het type HC595 zitten er op het LED board? 2
octet0 octet1
b00000000 0x0 b00000001 0x1

Druk op een toets om de bytes te verzenden
#v-

Not a problem there, and neither in other directories I made links in,
or copied the file to, only in that one directory..

Anyone have ideas?
Tnx.

Theo
 
J

John Machin

Goodday,

Something strange going on here.
A piece of code I wrote bombs out in one of de directories under $HOME,
but not in others. [snip]
def shiftout(numoctets):
os.system('clear')
ser = serial.Serial(0)
from array import array
octarray = [0]
for i in range(0,numoctets-1):
octarray.append(0)

for octet in range(numoctets,0,-1):
octarray[octet-1]=1
for shift in range(0,8):
strg = array("B",octarray).tostring()

The above statement appears to be where the error manifests itself.
Possibilities: (1) array is bound to a list (2) the result of
array("B", octarray) has an attribute tostring which is bound to a
list. Option (1) seems less implausible. I'd be replacing that line
by:

print "octet %r, shift %r, array %r" % (octet, shift, array)
array_b = array("B", octarray)
print "array_b %r" % array_b
print "array_b.tostring %r" % array_b.tostring
strg = array_b.tostring()

You haven't shown us all of your code -- is array mentioned elsewhere?
What other imports are you doing? Do you have a file called array.py
in the offending directory? [I believe that this wouldn't matter,
because builtin modules like array can't be overridden by a file-based
module of the same name, but I could be wrong]

What platform and what version of Python?

HTH,
John
 
C

Carsten Haese

Goodday,

Something strange going on here.
A piece of code I wrote bombs out in one of de directories under $HOME,
but not in others.

This usually means that the directory where your script doesn't work
contains some .py file that has the same name as a standard library
module that you need. Since the only import in your code snippet is
"from array import array", and calling array() raises the exception that
a list object is not callable, I am guessing that the culprit is a file
named array.py that defines a list called array. Rename that file.

-Carsten
 
T

Theo v. Werkhoven

The carbonbased lifeform John Machin inspired comp.lang.python with:
Goodday,

strg = array("B",octarray).tostring()

The above statement appears to be where the error manifests itself.
Possibilities: (1) array is bound to a list (2) the result of
array("B", octarray) has an attribute tostring which is bound to a
list. Option (1) seems less implausible. I'd be replacing that line
by:

print "octet %r, shift %r, array %r" % (octet, shift, array)
array_b = array("B", octarray)
print "array_b %r" % array_b
print "array_b.tostring %r" % array_b.tostring
strg = array_b.tostring()

You haven't shown us all of your code -- is array mentioned elsewhere?
What other imports are you doing? Do you have a file called array.py
in the offending directory? [I believe that this wouldn't matter,
because builtin modules like array can't be overridden by a file-based
module of the same name, but I could be wrong]

Bingo!
#v+
theo:/home/theo/Devel/Python $ ls array*
array.py array.pyc

theo:/home/theo/Devel/Python $ cat array.py
#!/usr/bin/python

import sys, os

array = [14, 8765, 756, 5345, 98, 5634654, 234123, 9087, 58, 297, 7865]
num = 0

while 1:
try:
print num
num = num + array.pop()
except:
break
#v-
And that code produces the numbers I was seeing..
What platform and what version of Python?

$ python --version
Python 2.5

$ uname -srv
Linux 2.6.18.8-0.1-default #1 SMP Fri Mar 2 13:51:59 UTC 2007
$ uname -mip
i686 athlon i386
HTH,
John

Thank you very much John. Nice catch.

Theo
 
T

Theo v. Werkhoven

The carbonbased lifeform Carsten Haese inspired comp.lang.python with:
This usually means that the directory where your script doesn't work
contains some .py file that has the same name as a standard library
module that you need. Since the only import in your code snippet is
"from array import array", and calling array() raises the exception that
a list object is not callable, I am guessing that the culprit is a file
named array.py that defines a list called array. Rename that file.

And you were right on the spot too, Cartsen, see my reply to John.
Thank you too.

Cheers,
Theo
 
J

John Machin

The carbonbased lifeform John Machin inspired comp.lang.python with:


The above statement appears to be where the error manifests itself.
Possibilities: (1) array is bound to a list (2) the result of
array("B", octarray) has an attribute tostring which is bound to a
list. Option (1) seems less implausible. I'd be replacing that line
by:
print "octet %r, shift %r, array %r" % (octet, shift, array)
array_b = array("B", octarray)
print "array_b %r" % array_b
print "array_b.tostring %r" % array_b.tostring
strg = array_b.tostring()
You haven't shown us all of your code -- is array mentioned elsewhere?
What other imports are you doing? Do you have a file called array.py
in the offending directory? [I believe that this wouldn't matter,
because builtin modules like array can't be overridden by a file-based
module of the same name, but I could be wrong]

Bingo!
#v+
theo:/home/theo/Devel/Python $ ls array*
array.py array.pyc

theo:/home/theo/Devel/Python $ cat array.py
#!/usr/bin/python

import sys, os

array = [14, 8765, 756, 5345, 98, 5634654, 234123, 9087, 58, 297, 7865]
num = 0

while 1:
try:
print num
num = num + array.pop()
except:
break
#v-
And that code produces the numbers I was seeing..
What platform and what version of Python?

$ python --version
Python 2.5

$ uname -srv
Linux 2.6.18.8-0.1-default #1 SMP Fri Mar 2 13:51:59 UTC 2007

Very interesting. My first reaction to Theo's posting was to make a
confident declaration like Carsten did, but I couldn't simulate this
behaviour on Windows with Python 2.5.1 (or 2.1.3 for that matter) and
moreover the docs say:
"""
Details of the module searching and loading process are implementation
and platform specific. It generally involves searching for a ``built-
in'' module with the given name and then searching a list of locations
given as sys.path.
"""
(from http://docs.python.org/ref/import.html)

I'm now curious about the rationale for having/permitting such a
difference.

Here's what I get on Windows XP Pro SP2 with Python 2.5.1:
8<-------------------------------------------
C:\junk>type array.py
print "Gotcha!"

C:\junk>type arrayimport.py
import sys, pprint
print 'sys.path:'
pprint.pprint(sys.path)
import array
print 'array: %r' % array
C:\junk>\python25\python arrayimport.py
sys.path:
['C:\\junk',
'C:\\python25\\lib\\site-packages\\cheesecake-0.6.1-py2.5.egg',
'C:\\python25\\lib\\site-packages\\setuptools-0.6c5-py2.5.egg',
'C:\\python25\\python25.zip',
'C:\\python25\\DLLs',
'C:\\python25\\lib',
'C:\\python25\\lib\\plat-win',
'C:\\python25\\lib\\lib-tk',
'C:\\python25',
'C:\\python25\\lib\\site-packages']
array: <module 'array' (built-in)>
8<----------------------------------------------------------------------
 
C

Carsten Haese

Very interesting. My first reaction to Theo's posting was to make a
confident declaration like Carsten did, but I couldn't simulate this
behaviour on Windows with Python 2.5.1 (or 2.1.3 for that matter) and
moreover the docs say:
"""
Details of the module searching and loading process are implementation
and platform specific. It generally involves searching for a ``built-
in'' module with the given name and then searching a list of locations
given as sys.path.

My "confident declaration" was a lucky guess based on the available
evidence. You couldn't reproduce the OP's problem because array is a
built-in module on Windows, but on Linux it's an external module:

Python 2.5 (r25:51908, Oct 28 2006, 12:26:14)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.<module 'array' from '/usr/local/lib/python2.5/lib-dynload/array.so'>

-Carsten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top