struct, IEEE-754 and internal representation

2005-11-09 Thread ej

I ran into something today I don't quite understand and I don't know all the
nitty gritty details about how Python stores and handles data internally.
(I think) I understand why, when you type in certain floating point values,
Python doesn't display exactly what you typed (because not all decimal
numbers are exactly representable in binary, and Python shows you the full
precision of what is representable). For example:

>>> 0.9
0.90002

and

>>> 148.73
148.72

So, I think I've got a pretty good handle on what the struct module is all
about. Let's take that number, 148.73, and use struct functions to look at
the raw bit pattern of what would be in a 32-bit register using IEEE754
float representation:

>>> hex(unpack('L', pack('f', x))[0])
'0x4314BAE1L'

That is, the four bytes representing this are 0x43, 0x14, 0xBA, 0xE1

Now let's go back the other way, starting with this 32 bit representation,
and turn it back into a float:

>>> unpack('>f', pack('', 0x43, 0x14, 0xBA, 0xE1))[0]
148.72999572753906


H...  Close, but I seem to be losing more the I would expect here.  I
initially thought I should be able to get back to at least what python
previously displayed:  148.72

I know there are 23 bits of mantissa in an IEEE-754, with a free '1'...

>>> hex(14872999)
'0xe2f1a7'

Looks like it takes 6 * 4 = 24 bits to represent that as an int

I am starting to think my expectation is wrong...

If that's true, then I guess I am confused why Python is displaying
148.72999572753906 when you unpack the 4 bytes, implying a lot more
precision that was available in the original 32-bits?   Python is doing
64-bit floating point here? I'm obviously not understanding something...
help?

-ej




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: struct, IEEE-754 and internal representation

2005-11-09 Thread ej
Ah! Well! That explains it. I started to suspect that but (obviously) did
not know that. LOL

Thanks for your prompt reply, Grant.  :)

-ej


-- 
http://mail.python.org/mailman/listinfo/python-list


exceptions, internals (introspection?)

2005-11-10 Thread ej

I'm trying to figure out how to get at some of the internal interpreter
state for exception handlers and debug statements in general.
I just read through Section 8 of the Python Tutorial. I see how to catch an
exception, and specify an optional argument to get ahold of the exception
itself. For example:

try:
{}['foo']
except Exception, x:
print "class of x =", x.__class__
print "type(x) =", type(x)
print "dir(x) =", dir(x)


If you don't handle an exception, the interpreter will quit and print a
stack trace. What I'm not seeing is how to handle the exception, but still
get the stack trace as a string so I could dump it to a log file or email it
or something.

I have often wondered how to get at other internals, such as the name of
the current function, file, line number I am in?  The arguments to the
current function, etc.  I browsed through the table of contents of both the
Library Reference & Language Reference. I see section 18. Python Language
Services.  In browsing through that, I'm thinking "Oh man... this is way
more than I need - there's got to be an easier way."   Nothing else is
jumping out at me. Can someone point me to some documentation on this
subject and/or provide some examples?

Thanks,  :)
-ej


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exceptions, internals (introspection?)

2005-11-10 Thread ej
"Paul Rubin" <http://[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> It's messy.  Look at sys.exc_info() and go from there.

Yeah, I think I am starting to see what you mean...


#! /usr/local/bin/python

import sys

try:
{}['foo']
except Exception, x:
print "class of x =", x.__class__
print "type(x) =", type(x)
print "dir(x) =", dir(x)

print
(type_, value_, traceback_) = sys.exc_info()
print "type_ =", type_
print "value_ =", value_
print "traceback_ =", traceback_
for key in dir(traceback_):
print "traceback_.%s =" % key, eval("traceback_.%s" % key)

print "dir(frame) = ", dir(traceback_.tb_frame)


[EMAIL PROTECTED]:~/src/python/exceptions> foo
class of x = exceptions.KeyError
type(x) = 
dir(x) = ['__doc__', '__getitem__', '__init__', '__module__', '__str__',
'args']

type_ = exceptions.KeyError
value_ = 'foo'
traceback_ = 
traceback_.tb_frame = 
traceback_.tb_lasti = 18
traceback_.tb_lineno = 6
traceback_.tb_next = None
dir(frame) =  ['__class__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', 'f_back', 'f_builtins', 'f_code',
'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_globals', 'f_lasti',
'f_lineno', 'f_locals', 'f_restricted', 'f_trace']



But at least that is something to go on. Thanks for your reply!

-ej


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exceptions, internals (introspection?)

2005-11-10 Thread ej
"Fredrik Lundh" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]

> the second example on this page shows you how to do that:
>
> http://effbot.org/librarybook/traceback

I am looking at it. Thanks for your prompt reply, Mr. Lundh! :)

-ej


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python commmand line params from c++

2005-06-30 Thread ej
Ummm, just a guess from looking at the output (not familiar with C++
interface)...
the error seems to complain about what's in someScript.py, not the call to
it.
What's in someScript.py?


"Wesley Henwood" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> What is the proper way to pass command line parameters to a python
> script called from C++? I'm tryng this:
>
> path = "c:\\someDir\\someScript.py param1 param2 param3";
> PyRun_SimpleFile(PyFile_AsFile( PyFile_FromString( path, "r")),
> "someScript.py");
>
> I'm getting a "format error someScript.py, line 1" when the code is
> executed.
>
> Note: The strange appearannce of the 3 python function calls nested is
> not a bug, but required to prevent a run-time error.
>


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subclassing a module: misleading(?) error message

2007-01-11 Thread ej

Carl Banks wrote:

 A good explanation

I have not been able to get back to news lately - lot going on, but
thank you for your time to explain that to me. It mostly makes pretty
good sense to me, but I will have to study metaclasses further. ;)

Thanks,
-ej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python FTP - NameError: name 'mydpa' is not defined

2008-08-05 Thread Ej

I have a similar problem. I need to download the same file every hour
so it will be nice to be able to rename the downloads with a variable
name.

For example in this case:

from ftplib import FTP
ftp=FTP('tgftp.nws.noaa.gov')
ftp.login()
ftp.cwd('SL.us008001/DF.of/DC.radar/DS.81dpr/SI.kbuf')
ftp.retrbinar('RETR sn.last', open('sn','wb').write)
ftp.quit()

Question: How to achive rename the downloaded files as sn1, sn2,
sn3 or with a timestamp?
The ftp.rename() is used to rename files on the server, isn't it?I'm
Python shy. Please help.

Any suggestion will be appreciated.





On Jul 28, 11:41 am, MRAB <[EMAIL PROTECTED]> wrote:
> On Jul 28, 2:27 pm, "Harry" <[EMAIL PROTECTED]> wrote:
>
> > Hi there. I am trying to download a file(sn.last) from a public FTP
> > server with the following code:
>
> > from ftplib import FTP
> > ftp=FTP('tgftp.nws.noaa.gov')
> > ftp.login()
> > ftp.cwd('SL.us008001/DF.of/DC.radar/DS.81dpr/SI.kbuf')
> > ftp.retrbinar('RETR sn.last', open(mydpa,'wb').write)
>
> open(mydpa,'wb') would open the file to which you're saving the data,
> but you haven't defined to mydpa, so Python will complain.
>
> > ftp.quit()
>
> > but got an error message, which I think is normal:
>
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >     ftp.retrbinary('RETR sn.last', open(mydpa,'wb').write)
> > NameError: name 'mydpa' is not defined
>
> Yep!
>
>
>
> > I don't know much about python, but just try to use the code to
> > download data. I don't know where the file will be saved to. Is the
> > mydata a file name or a folder name? where will it be saved to even
> > if it's working? Please help be fixed the problem step by step? I am
> > using 2.5.2 by the way.
>
> You need to tell it where to save the data by defining mydpa.
>
>
>
> > I really appreciate your help. Thanks.- Hide quoted text -
>
> - Show quoted text -

--
http://mail.python.org/mailman/listinfo/python-list


Re: Python FTP - NameError: name 'mydpa' is not defined

2008-08-05 Thread Ej
Hi Fredrik,

Thanks so much for you quick help!!! I googled the os.rename you gave
me and I found solution to solve my problem.

You made my day!




On Aug 5, 10:37 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Ej wrote:
> > I have a similar problem. I need to download the same file every hour
> > so it will be nice to be able to rename the downloads with a variable
> > name.
>
> > For example in this case:
>
> > from ftplib import FTP
> > ftp=FTP('tgftp.nws.noaa.gov')
> > ftp.login()
> > ftp.cwd('SL.us008001/DF.of/DC.radar/DS.81dpr/SI.kbuf')
> > ftp.retrbinar('RETR sn.last', open('sn','wb').write)
> > ftp.quit()
>
> > Question: How to achive rename the downloaded files as sn1, sn2,
> > sn3 or with a timestamp?
>
> you're downloading to the file name you pass to open ("sn" in your
> case), so to download to a different file, just pass in another name.
>
> to rename an existing file on the local file system, use os.rename.
>
> - Hide quoted text -
>
> - Show quoted text -

--
http://mail.python.org/mailman/listinfo/python-list


NOT Python: Taxonomy list reference

2006-03-21 Thread ej
I hope you will forgive this being a bit off subject, but this is about
the smartest list of people I know.  I came across a list in the past while
studying OO methdology stuff. The list was trying to make the point of the
problem classification systems often run into (i.e., often things belong in
more than one group simultaneously).

The list reads something like this:

1. Beasts that belong to the king
2. Great beasts
3. Furry beasts
4. Unicorns and other magical beasts.
5. Beasts with four hooves.
...

I can't remember if I was reading a web article, or one of my O'Reilly
books (I have many, many of these - not much hope of finding it by just
browsing through them), or one of my other books, or what... (I checked
Booch's "OO Analysis and Design" - didn't see it.) As great as Google is, I
have tried many different searches but am still coming up empty-handed.
Wikipedia is also pretty great, but again I can't seem to find it.

Any Pythonistas out there know the list I am referring to and could point me
to it?  Thanks for your time! :)


-- 
http://mail.python.org/mailman/listinfo/python-list


CGI module: get form name

2006-04-12 Thread ej

I'm not seeing how to get at the 'name' attribute of an HTML  element.

form = cgi.FieldStorage()

gives you a dictionary-like object that has keys for the various named
elements *within* the form...

I could easily replicate the form name in a hidden field, but there ought to
be some way to get directly at the form name but I'm just not seeing it. I
looked in the os.environ() - don't see it there.

Any ideas?

Thanks! :)

--
"I strongly believe what we're doing is the right thing. If I didn't believe
it-I'm going to repeat what I said before-I'd pull the troops out, nor if I
believed we could win, I would pull the troops out." --President George W.
Bush,  Charlotte, N.C., April 6, 2006


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Should I learn Python instead?

2006-04-14 Thread ej

"fyleow" <[EMAIL PROTECTED]> wrote :



> I realize that learning the library is part of the process, but as a
> beginner I appreciate simplicity.
> Is Python easier than C#?
Absolutely.

> Can someone show how to access an XML document on the web and have it
ready
> to be manipulated for comparison?
Yes. (see below)

> Any other advice for a newbie?
Learn Python. It's good to know other languages too, but when it comes to
getting stuff done fast & cleanly, you will find Python an invaluable tool.





# here's a simple use of the urllib module to fetch a document from the web
(output from an interactive python interpreter session):
> python
Python 2.3.4 (#1, Feb  7 2005, 15:50:45)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> url = 'http://www.google.com'
>>> fd = urllib.urlopen(url)
>>> html = fd.read()
>>> print html
Google