Re: Printing a hex character and prefixing it correctly
[email protected] wrote: > If I have an integer k, for instance; > > k = 32 // BASE 10 > > How do I get print to print it out in HEX and PREFIXED with 0x? What > is the PROPER WAY? > > This does not work: > > print "This is hex 32: ", int(k, 16) > > Xav How about this : k=32 print "This is hex 32: 0x%02X" % k Kind Regards, BgEddy -- http://mail.python.org/mailman/listinfo/python-list
Re: storing references instead of copies in a dictionary
mk wrote:
Calvin Spealman wrote:
To your actual problem... Why do you wanna do this anyway? If you want
to change the function in the dictionary, why don't you simply define
the functions you'll want to use, and change the one you have bound to
the key in the dictionary when you want to change it? In other words,
define them all at once, and then just d['1'] = new_f1. What is wrong
with that?
Well, basically nothing except I need to remember I have to do that.
Suppose one does that frequently in a program. It becomes tedious. I
think I will define some helper function then:
>>> def helper(fundict, newfun):
... fundict[newfun.func_name] = newfun
...
_If_ there were some shorter and still "proper" way to do it, I'd use
it. If not, no big deal.
For completeness:
def new_f1(arg):
return "NEW f1 " + arg
f1.func_code = new_f1.func_code
Don't use that unless you really have to and I nearly promise that you
don't.
I promise I won't use it. :-) It seems like a 'wrong thing to do'.
Well it's probably totally "non pythonic" but this does what you want:
def f2(arg):
return "f2 "+arg
def f1(arg):
return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]
Don't know if this is any use to you..
--
http://mail.python.org/mailman/listinfo/python-list
Re: storing references instead of copies in a dictionary
castironpi wrote:
On Jul 17, 10:05 am, mk <[EMAIL PROTECTED]> wrote:
def f2(arg):
return "f2 "+arg
def f1(arg):
return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]
Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
I didn't think of that.
Don't know if this is any use to you..
At least I learned something. :-)
You want consistent access to a changing variable. Wrap it in an
object:
a= Blank( )
a.ref= 'X'
a.ref
'X'
b= a
b.ref
'X'
a.ref= 'Y'
b.ref
'Y'
My "old fashioned" programing paradigms think of this in terms of
"pointers", a throw back to my schooling in 'C'. I find this general
form of problem to be common across languages and in some ways hard to
express in python. The whole idea of labels bound to objects is quite
alien to traditional terminology. I find one of the main attractions of
python is this new mindset that the language makes you adopt - a
different set of tools are at hand for the old school programmer.
castironpi - please give an example of what you are thinking as I find
this interesting. preferably post some brief example code.
--
http://mail.python.org/mailman/listinfo/python-list
Re: storing references instead of copies in a dictionary
bgeddy wrote:
castironpi wrote:
On Jul 17, 10:05 am, mk <[EMAIL PROTECTED]> wrote:
def f2(arg):
return "f2 "+arg
def f1(arg):
return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]
Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
I didn't think of that.
Don't know if this is any use to you..
At least I learned something. :-)
You want consistent access to a changing variable. Wrap it in an
object:
a= Blank( )
a.ref= 'X'
a.ref
'X'
b= a
b.ref
'X'
a.ref= 'Y'
b.ref
'Y'
My "old fashioned" programing paradigms think of this in terms of
"pointers", a throw back to my schooling in 'C'. I find this general
form of problem to be common across languages and in some ways hard to
express in python. The whole idea of labels bound to objects is quite
alien to traditional terminology. I find one of the main attractions of
python is this new mindset that the language makes you adopt - a
different set of tools are at hand for the old school programmer.
castironpi - please give an example of what you are thinking as I find
this interesting. preferably post some brief example code.
castironpi - please forgive the double post but my newsreader didn't
display your code correctly.. Doh !! Anyway - a nice way of addressing
the problem. However the OP's post revolved around having a rewritable
set of "labels" - which could be recorded at one time and when re
referenced the new definitions of those labels would be used. For
example a "collection" (list,dictionary,tuple) could be made of these
"labels" and then the underlying code accessed by the labels changed. If
the code was now ran indirectly by referencing the list then the new
code would be ran. These building blocks are how parsers are built and
the basis of language.
I can see how you form two ways of addressing the variable but can't
figure how this fits the original problem. Please elaborate for my
ignorance.
EdH.
--
http://mail.python.org/mailman/listinfo/python-list
Re: how do I know if I'm using a debug build of python
Tim Mitchell wrote: > Hi, > > A quick question: > Is there any way for a python script to know if it's being executed by a > debug build of python (python_d.exe) instead of python? > > Thanks > Tim > Not sure what this returns in Windows as I run Linux but this returns to namer of the python executable for my setup - maybe its what you want. import sys print sys.executable - will print out the name of the python executable. -- http://mail.python.org/mailman/listinfo/python-list
Re: Retrieving int from hex in a file.
Filipe Teixeira wrote:
Hi.
I have to open a binary file from an old computer and recover the
information stored (or at least try to). I use:
f=open('file.bin','rb')
a=f.read()
f.close()
a in now a string full of hex representations in the form:
a[6]='\x14'
a[7]='\x20'
I would like to convert these hex representations to int, but this
(the most obvious way) doesn't seem to be working
q=a[6]
q
'\x14'
int(q,16)
Traceback (most recent call last):
File "", line 1, in ?
ValueError: invalid literal for int():
How can I do this?
Thanks
As you say you are trying to recover information from the old computer
are you sure you want to convert the data to it's integer representation ?
What I mean is in this case the string will contain the actual data read
from the file, not a hex representation. It's just the python displays
this data as '\x14'. BTW - are you sure it's displaying '\x20' from your
data as this should be a space character i.e. " ".
In the python interpreter try this:
mydata=""
for i in range(256):
mydata+=chr(i)
>> mydata
This will show you the hex representation of the values in mydata that
it can't display however the string is not a string of "hex values" as
such, it contains the binary values 0-255.
--
http://mail.python.org/mailman/listinfo/python-list
