Connecting Tech Pros Worldwide Forums | Help | Site Map

help please

gargonx@gmail.com
Guest
 
Posts: n/a
#1: Jul 18 '05
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?


Erik Max Francis
Guest
 
Posts: n/a
#2: Jul 18 '05

re: help please


gargonx@gmail.com wrote:
[color=blue]
> UnboundLocalError: local variable 't2' referenced before assignment[/color]

...
[color=blue]
> 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?[/color]

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
...

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
I am become death, the destroyer of worlds.
-- J. Robert Oppenheimer (quoting Hindu legend)
Steven Bethard
Guest
 
Posts: n/a
#3: Jul 18 '05

re: help please


gargonx@gmail.com wrote:[color=blue]
> 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[/color]

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
Steven Bethard
Guest
 
Posts: n/a
#4: Jul 18 '05

re: help please


Erik Max Francis wrote:[color=blue]
> gargonx@gmail.com wrote:
>[color=green]
>> UnboundLocalError: local variable 't2' referenced before assignment[/color]
>
> ...
>[color=green]
>> t2=""
>>
>> def Proc(text): # "text" is some random text or use OrigText
>> ...[/color]
>
> The fix is to declare t2 global at the top of Proc:
>
> def Proc(text):
> global t2
> ...[/color]

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
wittempj@hotmail.com
Guest
 
Posts: n/a
#5: Jul 18 '05

re: help please


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

gargonx
Guest
 
Posts: n/a
#6: Jul 18 '05

re: help please


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)

gargonx
Guest
 
Posts: n/a
#7: Jul 18 '05

re: help please


Thanks that works very well

Peter Hansen
Guest
 
Posts: n/a
#8: Jul 18 '05

re: help please


wittempj@hotmail.com wrote:[color=blue]
> add just below the procedure declaration 'global t2' as you're
> referring to a global variable ...[/color]

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

-Peter
Steven Bethard
Guest
 
Posts: n/a
#9: Jul 18 '05

re: help please


gargonx wrote:[color=blue]
> 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)
>[/color]


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
gargonx
Guest
 
Posts: n/a
#10: Jul 18 '05

re: help please


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.

Steven Bethard
Guest
 
Posts: n/a
#11: Jul 18 '05

re: help please


gargonx wrote:[color=blue]
> 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.[/color]

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
gargonx
Guest
 
Posts: n/a
#12: Jul 18 '05

re: help please


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!

Steven Bethard
Guest
 
Posts: n/a
#13: Jul 18 '05

re: help please


gargonx wrote:[color=blue]
> 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).[/color]

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
gargonx
Guest
 
Posts: n/a
#14: Jul 18 '05

re: help please


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

Steven Bethard
Guest
 
Posts: n/a
#15: Jul 18 '05

re: help please


gargonx wrote:[color=blue]
> 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"[/color]

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


gargonx
Guest
 
Posts: n/a
#16: Jul 18 '05

re: help please


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

Steven Bethard
Guest
 
Posts: n/a
#17: Jul 18 '05

re: help please


gargonx wrote:[color=blue]
> 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[/color]

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
gargonx
Guest
 
Posts: n/a
#18: Jul 18 '05

re: help please


Even if i put it in exactly the way you did:
[color=blue][color=green][color=darkred]
>>> 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])[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: keys

Steven Bethard
Guest
 
Posts: n/a
#19: Jul 18 '05

re: help please


gargonx wrote:[color=blue]
> Even if i put it in exactly the way you did:
>[color=green][color=darkred]
>>>>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])[/color][/color]
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: keys[/color]

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 (mhammond@skippinet.com.au) -
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
Dennis Lee Bieber
Guest
 
Posts: n/a
#20: Jul 18 '05

re: help please


On 20 Feb 2005 20:12:50 -0800, "gargonx" <gargonx@gmail.com> declaimed
the following in comp.lang.python:
[color=blue][color=green][color=darkred]
> >>> decode_replacements.update([(std[key], key) for key in std])[/color][/color]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: keys[/color]

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]))
[color=blue][color=green][color=darkred]
>>> std = {"S":"H", "B":"v"}
>>> decode_replacements = {}
>>> print std[/color][/color][/color]
{'S': 'H', 'B': 'v'}[color=blue][color=green][color=darkred]
>>> decode_replacements.update(dict([(std[key], key) for key in std]))[/color][/color][/color]
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: keys[color=blue][color=green][color=darkred]
>>> decode_replacements.update(dict([(std[key], key) for key in std]))
>>> print decode_replacements[/color][/color][/color]
{'H': 'S', 'v': 'B'}[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
--[color=blue]
> ================================================== ============ <
> wlfraed@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulfraed@dm.net | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <[/color]
Steven Bethard
Guest
 
Posts: n/a
#21: Jul 18 '05

re: help please


Dennis Lee Bieber wrote:[color=blue]
> On 20 Feb 2005 20:12:50 -0800, "gargonx" <gargonx@gmail.com> declaimed
> the following in comp.lang.python:
>[color=green][color=darkred]
>>>>>decode_replacements.update([(std[key], key) for key in std])[/color]
>>
>>Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>>AttributeError: keys[/color]
>
> Did you read the reference manual?[/color]

Yes, actually. But I generally check only the reference manual for the
current version of Python:

http://docs.python.org/lib/typesmapping.html

which does support sequence arguments for dict.update. As noted there,
of course, this support was added for Python 2.4.

STeVe
rzed
Guest
 
Posts: n/a
#22: Jul 18 '05

re: help please



"gargonx" <gargonx@gmail.com> wrote in message
news:1108959170.585091.202240@z14g2000cwz.googlegr oups.com...[color=blue]
> Even if i put it in exactly the way you did:
>[color=green][color=darkred]
> >>> 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])[/color][/color]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: keys[/color]


Works with 2.4, but not with 2.3.4:

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.[color=blue][color=green][color=darkred]
>>> 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])
>>>
>>> print decode_replacements[/color][/color][/color]
{'H': 'S'}



Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.[color=blue][color=green][color=darkred]
>>> 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])[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: keys[color=blue][color=green][color=darkred]
>>>
>>> print decode_replacements[/color][/color][/color]
{}




Closed Thread