473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Program runs in all directories, except one..


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
--
theo at van-werkhoven.nl ICQ:277217131 SuSE Linux
linuxcounter.org: 99872 Jabber:muadib at jabber.xs4all.nl AMD XP3000+ 1024MB
"ik _heb_ niets tegen Microsoft, ik heb iets tegen
de uitwassen *van* Microsoft"
Apr 28 '07 #1
6 1308
On Apr 28, 9:50 pm, "Theo v. Werkhoven" <t...@van-
werkhoven.nl.invalidwrote:
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

Apr 28 '07 #2
On Sat, 2007-04-28 at 13:50 +0200, Theo v. Werkhoven wrote:
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
Apr 28 '07 #3
The carbonbased lifeform John Machin inspired comp.lang.python with:
On Apr 28, 9:50 pm, "Theo v. Werkhoven" <t...@van-
werkhoven.nl.invalidwrote:
>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
--
theo at van-werkhoven.nl ICQ:277217131 SuSE Linux
linuxcounter.org: 99872 Jabber:muadib at jabber.xs4all.nl AMD XP3000+ 1024MB
"ik _heb_ niets tegen Microsoft, ik heb iets tegen
de uitwassen *van* Microsoft"
Apr 28 '07 #4
The carbonbased lifeform Carsten Haese inspired comp.lang.python with:
On Sat, 2007-04-28 at 13:50 +0200, Theo v. Werkhoven wrote:
>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.
And you were right on the spot too, Cartsen, see my reply to John.
Thank you too.

Cheers,
Theo
--
theo at van-werkhoven.nl ICQ:277217131 SuSE Linux
linuxcounter.org: 99872 Jabber:muadib at jabber.xs4all.nl AMD XP3000+ 1024MB
"ik _heb_ niets tegen Microsoft, ik heb iets tegen
de uitwassen *van* Microsoft"
Apr 28 '07 #5
On Apr 29, 5:04 am, "Theo v. Werkhoven" <t...@van-
werkhoven.nl.invalidwrote:
The carbonbased lifeform John Machin inspired comp.lang.python with:
On Apr 28, 9:50 pm, "Theo v. Werkhoven" <t...@van-
werkhoven.nl.invalidwrote:
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
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<----------------------------------------------------------------------

Apr 28 '07 #6
On Sat, 2007-04-28 at 14:54 -0700, John Machin wrote:
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.
>>import array
array
<module 'array' from '/usr/local/lib/python2.5/lib-dynload/array.so'>

-Carsten
Apr 29 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
22
by: Brad Tilley | last post by:
Is it possible to write a file open, then read program in C and then call the C program from a Python script like this: for root, files, dirs in os.walk(path) for f in files: try:...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
6
by: Billy Jacobs | last post by:
I have a website which has both secure and non-secure pages. I want to uses forms authentication. How do I accomplish this? Originally I had my web.config file in the root with Forms...
34
by: kevin.watters | last post by:
Hi all, I have a need for a short program: Given a drive letter, it would recursively search through all directories, "generating" each filename that it encounters (need to pass each filename...
1
by: csumner | last post by:
Does anyone know if how i can require authentication for all directories on my website except for the root directory, without having to put a web.config file in each one? (The documentation...
5
by: rbt | last post by:
What is the most efficient way to recursively remove files and directories? Currently, I'm using os.walk() to unlink any files present, then I call os.walk() again with the topdown=False option...
4
by: Chuck P | last post by:
Using vs05 and making an asp.net 2.0 website. We use VSS and have everything organized like this SolutionName Documents Code WebProjectName1 WebServiceProjectName1
334
by: Antoninus Twink | last post by:
The function below is from Richard HeathField's fgetline program. For some reason, it makes three passes through the string (a strlen(), a strcpy() then another pass to change dots) when two would...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.