help please

G

gargonx

would anyone like to help to fugure out this problem i'm having here's
a portion of my code:

"""
I have three dictionaries along with this(you can probally spot what
they are), but just in case here are some testers:
"""
std = {
"b":"bo"
}

ext = {
"aa":"i"
}

punc = {
",":"!"
}
"""
when i run this i get :

UnboundLocalError: local variable 't2' referenced before assignment

"""

OrigText="ba, baa bo."

t2=""

def Proc(text): # "text" is some random text or use OrigText
for word in text:
for letter in word:
if letter in std.keys():
letter=std[letter]
t2=t2+letter # the problem is referene to this
elif letter in ext.keys():
letter=ext[letter]
t2=t2+letter
elif letter in punc.keys():
letter=punc[letter]
t2=t2+letter

can anyone figure out why t2 is not being used properly?
 
E

Erik Max Francis

UnboundLocalError: local variable 't2' referenced before assignment
...

t2=""

def Proc(text): # "text" is some random text or use OrigText
for word in text:
for letter in word:
if letter in std.keys():
letter=std[letter]
t2=t2+letter # the problem is referene to this
elif letter in ext.keys():
letter=ext[letter]
t2=t2+letter
elif letter in punc.keys():
letter=punc[letter]
t2=t2+letter

can anyone figure out why t2 is not being used properly?

The problem is that you're not making it clear to Python that you want
the t2 mentioned in Proc to reference the global t2, rather than a
local. It's concluding the latter, whereas you mean the former. You
can probably see that as a local, t2 is indeed referenced before it's
assigned (t2 = t2 + letter). The fix is to declare t2 global at the top
of Proc:

def Proc(text):
global t2
...
 
S

Steven Bethard

t2=""

def Proc(text): # "text" is some random text or use OrigText
for word in text:
for letter in word:
if letter in std.keys():
letter=std[letter]
t2=t2+letter # the problem is referene to this
elif letter in ext.keys():
letter=ext[letter]
t2=t2+letter
elif letter in punc.keys():
letter=punc[letter]
t2=t2+letter

As written, t2 is a global because of the statement at the top:

t2=""

Inside Proc, the statement:

t2=t2+letter # the problem is referene to this

declares t2 as local to the function. (Assignment to a name inside a
function declares that name as local to the function.) You could use
the global keyword, but a better approach would be something like:

replacements = std.items() + ext.items() + punc.items()

def proc(text):
result = []
for word in text:
for k, v in replacements:
word = word.replace(k, v)
result.append(word)
return ''.join(result)

Now, instead of using a global 't2', I simply create a string and return
it. Note that I've also replaced your inefficient string addition with
the more efficient list-append and list-join.

Steve
 
S

Steven Bethard

Erik said:
The fix is to declare t2 global at the top of Proc:

def Proc(text):
global t2
...

But please don't. The global declaration is a wart of Python, and in
most cases, your code will be much cleaner if you avoid it. Certainly
in this case, making t2 a local of the function is much more sensible.
If you stick with your current implementation (which I advise against),
you should write it something like:

def proc(text):
t2 = ""
...
# your Proc code here
...
return t2

Steve
 
W

wittempj

add just below the procedure declaration 'global t2' as you're
referring to a global variable ...
 
G

gargonx

This works much better, aside from the fact that it does'nt work for
the std dictionary. the letters used from here stay the same. that
dictionary looks like this:

std = {
"A":"Z",
"Z":"A",
"B":"Y",
"Y":"B",
"C":"X",
"X":"C",
"E":"V",
"V":"E",
"H":"S",
"S":"H",
"M":"N",
"N":"M"
}

what could be causing this?

i did figure out that if you reverse the k,v you are able to get it but
that doesn't turn up the results i need

def proc(text):
result = []
for word in text:
for k, v in replacements:
word = word.replace(v,k) #here i reversed them
result.append(word)
return ''.join(result)
 
P

Peter Hansen

add just below the procedure declaration 'global t2' as you're
referring to a global variable ...

More specifically, *modifying* it. Just referring to it
doesn't require a "global" declaration...

-Peter
 
S

Steven Bethard

gargonx said:
This works much better, aside from the fact that it does'nt work for
the std dictionary. the letters used from here stay the same. that
dictionary looks like this:

std = {
"A":"Z",
"Z":"A",
"B":"Y",
"Y":"B",
"C":"X",
"X":"C",
"E":"V",
"V":"E",
"H":"S",
"S":"H",
"M":"N",
"N":"M"
}

what could be causing this?

i did figure out that if you reverse the k,v you are able to get it but
that doesn't turn up the results i need

def proc(text):
result = []
for word in text:
for k, v in replacements:
word = word.replace(v,k) #here i reversed them
result.append(word)
return ''.join(result)


The problem is that if you run all the replacements in std, you first
translate As to Zs and then Zs to As. If you want to do each character
only once, you should probably write this as:

py> std = {
.... "A":"Z",
.... "Z":"A",
.... "B":"Y",
.... "Y":"B",
.... "C":"X",
.... "X":"C",
.... "E":"V",
.... "V":"E",
.... "H":"S",
.... "S":"H",
.... "M":"N",
.... "N":"M"}
py> ext = {"aa":"i"}
py> punc = {",":"!"}
py> replacements = {}
py> replacements.update(punc)
py> replacements.update(ext)
py> replacements.update(std)
py> def proc(text):
.... result = []
.... for char in text:
.... result.append(replacements.get(char, char))
.... return ''.join(result)
....
py> proc('ABCDEFG')
'ZYXDVFG'

Or alternatively:

py> def proc(text):
.... return ''.join([replacements.get(c, c) for c in text])
....
py> proc('ABCDEFG')
'ZYXDVFG'

Note however, that this won't work for multi-character strings like you
had in 'ext' in your original example:

py> proc('ABCaaEFG')
'ZYXaaVFG'

But neither would your original code. Are the items in std always a
single character? Does ext always translate two characters to one?
Could you give us some more info on the task here?

Steve
 
G

gargonx

yes the items in std are always single to single, and ext single to
double. basicly the ext are refernce to the std itmes. the second
character in ext is a number depending on how far it is from the item
in std. this is just a simple encoding program.
 
S

Steven Bethard

gargonx said:
yes the items in std are always single to single, and ext single to
double. basicly the ext are refernce to the std itmes. the second
character in ext is a number depending on how far it is from the item
in std. this is just a simple encoding program.

If your keys are always single characters (as this description seems to
suggest) then the last code I posted should do what you want. Repeated
here for your enjoyment:

# merge mappings:
# std has highest precedence, then ext, then punc
replacements = {}
replacements.update(punc)
replacements.update(ext)
replacements.update(std)

# define encoding procedure
def proc(text):
# replace each character with its replacement
# or leave character unchanged if not in the mapping
return ''.join([replacements.get(c, c) for c in text])

Steve
 
G

gargonx

Well that seems to work like a champion, but my prob then would be; how
do i get the double character values of ext to turn back to the single
character keys. The reversed (decode if you will). Thanks a lot Steve
this has been a great learning!
 
S

Steven Bethard

gargonx said:
Well that seems to work like a champion, but my prob then would be; how
do i get the double character values of ext to turn back to the single
character keys. The reversed (decode if you will).

It's unclear what you want to do here. If you have say:

ext = dict(aa='A', ab='B', bb='C')

then how should 'aaabb' be decoded? Some possibilities:

'ABb'
'AaC'
'aAC'

Steve
 
G

gargonx

let's take the word "dogs"

ext = dict("D":"V1", "O":"M1", "G":"S1")
std = dict("S":"H")

encode("DOGS") # proc()
we'll get: "V1M1S1H"

let's say i want to do just the opposite
word: "V1M1S1H"
decode("V1M1S1H")
#how do i decode "V1" to "D", how do i keep the "V1" together?
and get: "DOGS"

#everything gets changed to uppercase
 
S

Steven Bethard

gargonx said:
let's take the word "dogs"

ext = dict("D":"V1", "O":"M1", "G":"S1")
std = dict("S":"H")

encode("DOGS") # proc()
we'll get: "V1M1S1H"

let's say i want to do just the opposite
word: "V1M1S1H"
decode("V1M1S1H")
#how do i decode "V1" to "D", how do i keep the "V1" together?
and get: "DOGS"

If you can make some assumptions about the right-hand sides of your
dicts, you can probably tokenize your string with a simple regular
expression:

py> import re
py> charmatcher = re.compile(r'[A-Z][\d]?')
py>
py> ext = dict(D="V1", O="M1", G="S1")
py> std = dict(S="H")
py>
py> decode_replacements = {}
py> decode_replacements.update([(std[key], key) for key in std])
py> decode_replacements.update([(ext[key], key) for key in ext])
py>
py> def decode(text):
.... return ''.join([decode_replacements.get(c, c)
.... for c in charmatcher.findall(text)])
....
py>
py> decode("V1M1S1H")
'DOGS'

So, instead of using
for c in text
I use
for c im charmatcher.findall(text)
That gives me the correct tokenization, and i can just use the inverted
dicts to map it back. Note however that I've written the regular
expression to depend on the fact that the values in std and ext are
either single uppercase characters or single uppercase characters
followed by a single digit.

Steve
 
G

gargonx

I think there's a problem with the code:

py> decode_replacements.update([(std[key], key) for key in std])
py> decode_replacements.update([(ext[key], key) for key in ext])

when i run this i get an error:
AttributeError: keys

I can't get that figured out
 
S

Steven Bethard

gargonx said:
I think there's a problem with the code:

py> decode_replacements.update([(std[key], key) for key in std])
py> decode_replacements.update([(ext[key], key) for key in ext])

when i run this i get an error:
AttributeError: keys

I can't get that figured out

Can you show the part of your code in which you do this? As you can see
from the interactive Python session I posted, I didn't get an
AttributeError...

STeVe
 
G

gargonx

Even if i put it in exactly the way you did:
import re
charmatcher = re.compile(r' [A-Z] [\d]?')

ext = dict(D="V1", O="M1", G="S1")
std = dict(S="H")

decode_replacements ={}
decode_replacements.update([(std[key], key) for key in std])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: keys
 
S

Steven Bethard

gargonx said:
Even if i put it in exactly the way you did:
import re
charmatcher = re.compile(r' [A-Z] [\d]?')

ext = dict(D="V1", O="M1", G="S1")
std = dict(S="H")

decode_replacements ={}
decode_replacements.update([(std[key], key) for key in std])

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: keys

What version of Python are you using? Here's what I get:

PythonWin 2.4 (#60, Nov 30 2004, 09:34:21) [MSC v.1310 32 bit (Intel)]
on win32.
Portions Copyright 1994-2004 Mark Hammond ([email protected]) -
see 'Help/About PythonWin' for further copyright information.
py>
py> import re
py> charmatcher = re.compile(r' [A-Z] [\d]?')
py>
py> ext = dict(D="V1", O="M1", G="S1")
py> std = dict(S="H")
py>
py> decode_replacements = {}
py> decode_replacements.update([(std[key], key) for key in std])

As you can see, I'm using Python 2.4. I'm guessing you're using an
earlier version. I just checked the docs for dict.update:

"update() accepts either another mapping object or an iterable of
key/value pairs (as a tuple or other iterable of length two). If keyword
arguments are specified, the mapping is then is updated with those
key/value pairs: "d.update(red=1, blue=2)". Changed in version 2.4:
Allowed the argument to be an iterable of key/value pairs and allowed
keyword arguments."

So dict.update() only accepts a list of key/value pairs as of 2.4 I
guess. Try:

py> decode_replacements.update(dict([(std[key], key) for key in std]))

I believe that, unlike dict.update, the dict constructor allowed a list
of key/value pairs previous to Python 2.4. If that doesn't work either,
you can do the more verbose version:

py> for key in std:
.... decode_replacements[std[key]] = key
....

STeVe
 
D

Dennis Lee Bieber

decode_replacements.update([(std[key], key) for key in std])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: keys

Did you read the reference manual?

.update() wants a DICTIONARY argument, you are creating a list
of tuples. So of course there are no keys available for update...

Try:

decode_replacements.update(dict([(std[key], key) for key in std]))
std = {"S":"H", "B":"v"}
decode_replacements = {}
print std {'S': 'H', 'B': 'v'}
decode_replacements.update(dict([(std[key], key) for key in std]))
Traceback (most recent call last):
File said:
decode_replacements.update(dict([(std[key], key) for key in std]))
print decode_replacements {'H': 'S', 'v': 'B'}
--
> ============================================================== <
> (e-mail address removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (e-mail address removed) | Bestiaria Support Staff <
> ============================================================== <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <
 

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
474,219
Messages
2,571,127
Members
47,744
Latest member
FrederickM

Latest Threads

Top