python newbie having strangeness

J

Joe F. Blow

The program below has been acting very weird. Last night it complained
that I was referencing X in getline() before defining it. Of course I
wasn't, as X is a global.

This morning I ran the exact same code again and now it says,
"NameError: name 'convert' is not defined". HUH????? Of course it's
defined!

What am I doing wrong? Any ideas? I'm using pythonwin and python2.3 on
windoze xp.

----------------------------------------------------------------------------------

x=1
count=binlist=0

#get s19 file in list named "line"
inp=file("c:\\download\\electron\\68hc11\\a code\\tankbot2.s19","r")
line = inp.read()
inp.close()

if line[x]=="0": #is command char a "0"?
dig1=line[x+1] #yes, get line length
dig2=line[x+2]
a=convert(dig1,dig2)
x=x+((a*2)+5) #point to command char of next line

if line[x]=="1":
getline()
print binlist

def getline():
global count
global x
global line
global binlist
dig1=line[x+1]
dig2=line[x+2]
a=convert(dig1,dig2)
x+=2
e=x
for e in range(e,e+a,2):
dig1=line[e+1]
dig2=line[e+2]
binlist[count]=convert(dig1,dig2)
count+=1

#convert ascii hex digits x & y to an integer
def convert(x,y):
a=int(x)
b=int(y)
a=a<<4
a=a+b
return(a)
 
?

=?ISO-8859-1?Q?J=F8rgen_Cederberg?=

Joe said:
The program below has been acting very weird. Last night it complained
that I was referencing X in getline() before defining it. Of course I
wasn't, as X is a global.

This morning I ran the exact same code again and now it says,
"NameError: name 'convert' is not defined". HUH????? Of course it's
defined!

What am I doing wrong? Any ideas? I'm using pythonwin and python2.3 on
windoze xp.

Hi

yes convert is defined, but after it is being used. See below and notice
how I have moved up before it is being used.

HTH
Jorgen Cederberg

> #convert ascii hex digits x & y to an integer
> def convert(x,y):
> a=int(x)
> b=int(y)
> a=a<<4
> a=a+b
> return(a)
#get s19 file in list named "line"
inp=file("c:\\download\\electron\\68hc11\\a code\\tankbot2.s19","r")
line = inp.read()
inp.close()

if line[x]=="0": #is command char a "0"?
dig1=line[x+1] #yes, get line length
dig2=line[x+2]
a=convert(dig1,dig2)
x=x+((a*2)+5) #point to command char of next line

if line[x]=="1":
getline()
print binlist

def getline():
global count
global x
global line
global binlist
dig1=line[x+1]
dig2=line[x+2]
a=convert(dig1,dig2)
x+=2
e=x
for e in range(e,e+a,2):
dig1=line[e+1]
dig2=line[e+2]
binlist[count]=convert(dig1,dig2)
count+=1
 
P

Paul McGuire

Joe F. Blow said:
The program below has been acting very weird. Last night it complained
that I was referencing X in getline() before defining it. Of course I
wasn't, as X is a global.

This morning I ran the exact same code again and now it says,
"NameError: name 'convert' is not defined". HUH????? Of course it's
defined!

What am I doing wrong? Any ideas? I'm using pythonwin and python2.3 on
windoze xp.

-------------------------------------------------------------------------- --------

x=1
count=binlist=0

#get s19 file in list named "line"
inp=file("c:\\download\\electron\\68hc11\\a code\\tankbot2.s19","r")
line = inp.read()
inp.close()

if line[x]=="0": #is command char a "0"?
dig1=line[x+1] #yes, get line length
dig2=line[x+2]
a=convert(dig1,dig2)
x=x+((a*2)+5) #point to command char of next line

if line[x]=="1":
getline()
print binlist

def getline():
global count
global x
global line
global binlist
dig1=line[x+1]
dig2=line[x+2]
a=convert(dig1,dig2)
x+=2
e=x
for e in range(e,e+a,2):
dig1=line[e+1]
dig2=line[e+2]
binlist[count]=convert(dig1,dig2)
count+=1

#convert ascii hex digits x & y to an integer
def convert(x,y):
a=int(x)
b=int(y)
a=a<<4
a=a+b
return(a)
Hey Joe -

Here are some keen Python features that might make your task easier:

file.readlines() - will give you each line of your file at a time, no need
to read it all at once (or even better, "for line in file('xyzzy.s19'):")
string slicing - line[x:x+2] will give you a 2-char substring at location x
hex to int conversion - int("0A",16) gives the value 10
list comprehensions (this is way cool!) - bytes = "AABBCCDD", bytelist = [
int(bytes[x:x+2],16) for x in range(0,len(bytes),2) ] results in a bytelist
containing [170, 187, 204, 221]

Python has been around a while, and before writing your own hex-to-int
conversion, it's worth trying to find a way to do it in the language. Soon
you'll graduate from brute force range looping to list comprehensions.

Good luck with your S19 work.

-- Paul
 
J

Joe F. Blow

Thanks for your help guys. Much appreciated. I will for sure have more
dumb questions for ya later. I _dabble_ in many languages (C, Perl
and lots of flavors of assembler especially), but just discovered
Python. Seems like a very nice language if I can just get my head
around a few things.
 
J

Joe F. Blow

list comprehensions (this is way cool!) - bytes = "AABBCCDD", bytelist = [
int(bytes[x:x+2],16) for x in range(0,len(bytes),2) ] results in a bytelist
containing [170, 187, 204, 221]

Wow!!! Tried this out tonight. Very cool! Obviously that's the way to
do it.

The python reference manual totally sucks. It's incredibly difficult
to find what you want in it and it seems to not even mention lots of
stuff. Maybe it's there, but I can't find it. List comprehensions are
barely mentioned, as far as I can tell. When you do find what you want
to know about, it seems like it's explained in as cryptic a way as
possible.

Aaaaarrrrrggggghhhh!!!


Python has been around a while, and before writing your own hex-to-int
conversion, it's worth trying to find a way to do it in the language. Soon

I looked. Read the above rant.
 
M

Mel Wilson

The python reference manual totally sucks. It's incredibly difficult
to find what you want in it and it seems to not even mention lots of
stuff. Maybe it's there, but I can't find it. List comprehensions are
barely mentioned, as far as I can tell. When you do find what you want
to know about, it seems like it's explained in as cryptic a way as
possible.

Aaaaarrrrrggggghhhh!!!

Yep. Concentrate on the tutorial and the Library
reference.

Actually the Reference manual is a good reference, but I
don't know anybody who could learn the language from it.

Regards. Mel.
 
T

Tony C

this may be of some use in converting from hex to decimal

file=open("file.text")
WholeFile= file.readlines()
WholeFile.close()

# now, you have to search for the location of the decimal numbers
# I can't specify how to do that, without knowing what your input file looks #like

#but once you have a string as in

HexStr ="123ABCDE" #implied is that this string was read from a file
(you MUST strip off any leading 0x, or trailing H or h, before proceeding)

You can convert it to a decimal string by
DecStr = "%lu" % long(HexStr,16)
and to an integer by
DecNum = long(DecStr)
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top