convert binary to float
I have tried and tried... I'd like to read in a binary file, convert it's 4 byte values into floats, and then save as a .txt file. This works from the command line (import struct); In [1]: f = open("test2.pc0", "rb") In [2]: tagData = f.read(4) In [3]: tagData Out[3]: '\x00\x00\xc0@' I can then do the following in order to convert it to a float: In [4]: struct.unpack("f", "\x00\x00\xc0@") Out[4]: (6.0,) But when I run the same code from my .py file: f = open("test2.pc0", "rb") tagData = f.read(4) print tagData I get this (ASCII??): └@ I only know how to work with '\x00\x00\xc0@'. I don't understand why the output isn't the same. I need a solution that will allow me to convert my binary file into floats. Am I close? Can anyone point me in the right direction? Thanks, Mason -- http://mail.python.org/mailman/listinfo/python-list
Re: convert binary to float
On Jun 1, 6:41 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Sun, 1 Jun 2008 12:55:45 -0700 (PDT), Mason > <[EMAIL PROTECTED]> declaimed the following in > comp.lang.python: > > > I have tried and tried... > > > I'd like to read in a binary file, convert it's 4 byte values into > > floats, and then save as a .txt file. > > > This works from the command line (import struct); > > > In [1]: f = open("test2.pc0", "rb") > > In [2]: tagData = f.read(4) > > In [3]: tagData > > Interpreter display of raw object name uses repr() > > > Out[3]: '\x00\x00\xc0@' > > > I can then do the following in order to convert it to a float: > > > In [4]: struct.unpack("f", "\x00\x00\xc0@") > > Out[4]: (6.0,) > > > But when I run the same code from my .py file: > > > f = open("test2.pc0", "rb") > > tagData = f.read(4) > > print tagData > > Display from a print statement uses str() > > > I get this (ASCII??): > > „@ > > Probably not ASCII -- ASCII doesn't have that spanish (?) bottom row > quote... And a pair of null bytes don't take up screen space. > > > I only know how to work with '\x00\x00\xc0@'. > > > I don't understand why the output isn't the same. I need a solution > > that will allow me to convert my binary file into floats. Am I close? > > Can anyone point me in the right direction? > > Why do you have to /see/ the byte representation in the first > place... just feed the four bytes to the struct module directly. > > import struct > fin = open("test2.pc0", "rb") > tagFloat = struct.unpack("f", fin.read(4))[0] > print tagFloat > -- > WulfraedDennis Lee Bieber KD6MOG > [EMAIL PROTECTED] [EMAIL PROTECTED] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [EMAIL PROTECTED]) > HTTP://www.bestiaria.com/ Thanks Dennis, I'm OK now. I just sort of dropped the ball for a bit :). Mason -- http://mail.python.org/mailman/listinfo/python-list
Re: convert binary to float
On Jun 1, 5:12 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > On Jun 1, 3:55 pm, Mason <[EMAIL PROTECTED]> wrote: > > > > > I have tried and tried... > > > I'd like to read in a binary file, convert it's 4 byte values into > > floats, and then save as a .txt file. > > > This works from the command line (import struct); > > > In [1]: f = open("test2.pc0", "rb") > > In [2]: tagData = f.read(4) > > In [3]: tagData > > Out[3]: '\x00\x00\xc0@' > > > I can then do the following in order to convert it to a float: > > > In [4]: struct.unpack("f", "\x00\x00\xc0@") > > Out[4]: (6.0,) > > > But when I run the same code from my .py file: > > > f = open("test2.pc0", "rb") > > tagData = f.read(4) > > print tagData > > > I get this (ASCII??): > > „@ > > Remembering to put that struct.unpack() call in your module might > help ;-) > > George Wow ... I did have it in there, but I forgot include it in my post. Anyway, this works just fine: f = open("test2.pc0", "rb") tagData = f.read(4) print struct.unpack("f", tagData) Thanks for waking me up George! -- http://mail.python.org/mailman/listinfo/python-list
Re: Running all unit tests
On Feb 6, 9:11 pm, Jason Voegele wrote: > I'm working on my first substantial Python project, and I'm following a fully > test-first approach. I'd like to know how Pythonistas typically go about > running all of their tests to ensure that my application stays "green". > > In Ruby, I would have a Rake task so that I could say "rake test" and all > tests would be executed. In C or C++ I would have a make target so I could > run all my tests with "make test". In Java it would be an Ant task and "ant > test". And so forth and so on. > > What's the recommended approach for Python programs? I'm sure I could write > a shell script (or a Python script even) that scans my "test" directory for > test cases and runs them, but I'm wondering if there's something already > built in that could do this for me. > > -- > Jason Voegele > Only fools are quoted. > -- Anonymous I don't know about the recommended approach, but I've done something like you suggest in a library I authored. Any files named test*.py are found and added to the unittest test suite. See http://code.google.com/p/pydicom/source/browse/trunk/source/dicom/test/run_tests.py. HTH Darcy -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Imaging Library (PIL) question
On Oct 20, 2:14 pm, Sid <[EMAIL PROTECTED]> wrote: > Hi, > > I am tryin to copy an image into my own data structure(a sort of 2d array > for further FFT). I've banged my head over the code for a couple of hours > now. The simplified version of my problem is below. > > #-Code > > import Image > pic = Image.open("Images/a.jpg") > (picdata,width,height) = (pic.load(),pic.size[0],pic.size[1]) > > picMap = [[0] * width ] * height #matrix needed for FFT > > print "--Printing PIC MAP--" > for h in range(0,height): > for w in range(0,width): > print picMap[h][w]," ", #everything initialized to ZERO...this is > ok > print "\n" > > print "--Copying to PIC MAP--" > for h in range(0, height): > for w in range(0, width): > picMap[h][w] = picdata[w,h] #problem lies here > print picMap[h][w]," ", #Seems to copy perfectly here as the > output shows > print "\n" > > print "--Printing PIC MAP AGAIN--" > for h in range(0,height): > for w in range(0,width): > print picMap[h][w]," ", #Should print the values as above, but > doesn't > print "\n" > > #-Code End > > Hopefully someone would take a look and let me know what i'm doing > something wrong here. > > Thanks a lot > -Sid The problem is likely to do with the line picMap = [[0] * width ] * height The first [0]*width creates a list, then the *height repeats the reference to the same list. See, for example http://mail.python.org/pipermail/tutor/2001-December/010414.html. It prints okay in the middle section because the items have just been stored. Later iterations in that same loop are replacing array elements that have already been printed. The link explains the problem and shows a way to avoid this. You might also consider using Numpy, it has explicit functions to zero any dimension array. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dummy explanation to win32com needed
On Oct 22, 3:43 pm, korean_dave <[EMAIL PROTECTED]> wrote: > Hi. I need a dummy's explanation to utilizing the win32com component > to access Microsoft Excel. > > So far, I have this code. > > import win32com.client > xl = win32com.client.Dispatch("Excel.Application") > xl.Visible = 1 > > workbook = xl.Workbooks.Open("C:\test.xls") > > Now, my question is, where do I find the snytax I can use to access > such functions like workbook.ActiveSheet.Cells(1,1).Value etc... > > I need a reference to the API I can utilize. > > Where can I find this? > > Thanks guys, > -Dave I haven't used this in quite some time, but if I recall correctly, it simply follows the object model as found in Excel Help | Visual Basic Reference, or go into the Excel code editor (alt-F11)and use the object browser. One thing to be aware of, IIRC, is that in VB you can leave out some default names (like .Value, .Item for collections) but these must be explicitly referenced when called using win32com. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there an official way to add methods to an instance?
And for such a behavior they've termed "monkeying" Thus, the coinage "Monkeypatching" for what you want to do: http://mail.python.org/pipermail/python-dev/2008-January/076194.html There are a group of people who think "monkeypatching is destroying ruby." You still probably should avoid it for production code. On Fri, Apr 4, 2008 at 11:25 AM, John Nagle <[EMAIL PROTECTED]> wrote: > Bruno Desthuilliers wrote: > > Paul Rubin a écrit : > >> Brian Vanderburg II <[EMAIL PROTECTED]> writes: > >>> I've checked out some ways to get this to work. I want to be able to > >>> add a new function to an instance of an object. > >> > >> Ugh. Avoid that if you can. > > > > Why so ? OO is about objects, not classes, and adding methods on a > > per-object basis is perfectly legitimate. > >It's what professional programmers call a "l33t feature", > one not suitable for production code. Typically such features > are used by programmers with about two years experience, > trying too hard to prove that they're cool. > >John Nagle > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Data structure recommendation?
If you can imply a partial order on your ranges then you can get O(n lg n) random access using a heap data structure. You'll have to implement your own heap, but heap search is easy to implement (it's Heapify that might require a little thinking). This will only work, of course, if your ranges are disjoint. C On Mon, Apr 7, 2008 at 4:58 PM, Steve Holden <[EMAIL PROTECTED]> wrote: > Steven Clark wrote: > > Hi all- > > > > I'm looking for a data structure that is a bit like a dictionary or a > > hash map. In particular, I want a mapping of floats to objects. > > However, I want to map a RANGE of floats to an object. > > > > This will be used for timestamped storage / lookup, where the float > > represents the timestamp. > > get(x) should return the object with the "newest" (biggest) timestamp > > y <= x, if it exists. > > Example: > > > > foo = Foo() > > > > foo.get(1.5) > > -> None > > foo.put(1.3, 'a') > > foo.put(2.6, 'b') > > foo.get(1.5) > > -> 'a' > > foo.get(7.8) > > -> 'b' > > foo.put(5.0, 'c') > > foo.get(7.8) > > -> 'c' > > > > In otherwords, by the end here, for foo.get(x), > > x < 1.3 maps to None, > > 1.3 <= x < 2.6 maps to 'a', > > 2.6 <= x < 5.0 maps to 'b', > > 5.0 <= x maps to 'c'. > > > > I know that foo.get() will be called many times for each foo.put(). Is > > there any way to achieve O(1) performance for foo.get(), maybe via > > some kind of hash function? Or is the best thing to use some kind of > > binary search? > > > I believe the best way to implement this would be a binary search > (bisect?) on the actual times, which would be O(log N). Though since > they are timestamps they should be monotonically increasing, in which > case at least you don't have to go to the expense of sorting them. > > "Some kind of hash function" won't hack it, since the purpose of a hash > function is to map a large number of (possibly) evenly-distributed > (potential) keys as nearly as possible randomly across a much smaller > set of actual values. > > You might try messing around with reducing the precision of the numbers > to home in on a gross region, but I am not convinced that does anything > other than re-spell binary search if carried to extremes. > > regards > Steve > -- > Steve Holden+1 571 484 6266 +1 800 494 3119 > Holden Web LLC http://www.holdenweb.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling Python functions from Excel
On Nov 15, 2:20 am, Cannonbiker wrote: > Please I need Calling Python functions from Excel and receive result > back in Excel. Can me somebody advise simplest solution please? I am > more VBA programmer than Python. A couple of years ago I used MSScriptControl for this. Couldn't find a great reference just now, but here is a discussion which should give enough information: http://www.velocityreviews.com/forums/t319222-re-python-in-excel.html Check from around message 3 on. -- http://mail.python.org/mailman/listinfo/python-list
Newbie thwarted by sys.path on Vista
I'm running Python 3.1 on Vista and I can't figure out how to add my own directory to sys.path. The docs suggest that I can either add it to the PYTHONPATH environment variable or to the PythonPath key in the registry. However, PYTHONPATH doesn't exist, and updating the registry key has no effect (and in any case the contents aren't the same as sys.path). So where does sys.path get its value from, and how do I change it? -- Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie thwarted by sys.path on Vista
"Dave Angel" wrote in message news:mailman.4120.1249172970.8015.python-l...@python.org... Michael M Mason wrote: I'm running Python 3.1 on Vista and I can't figure out how to add my own directory to sys.path. Thanks to Jon, Piet, David and Dave for the responses. sys.path gets its values from several places. Ah, I'd misunderstood the docs: I thought it came from just one place (which I couldn't find). Anyway, I'd suggest adding it to PythonPath, and if it's empty, just create it with the directory you need. Thanks--that worked! I'm hoping you know you can also add to sys.path directly during script initialization. It's just a list, and is writeable. Yes, but I'm mainly playing in IDLE and I was getting a bit fed up of repeatedly typing import sys sys.path.append('C:/Users/Michael/Code/Python') import mystuff -- Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie thwarted by sys.path on Vista
"Mark Lawrence" wrote in message news:mailman.4130.1249203322.8015.python-l...@python.org... Be careful, I'm screwed things up on several occasions by placing a file on PYTHONPATH that overrides a file in the standard library, test.py being my favourite! Thanks. Sure enough, I've already got my own test.py but I hadn't discovered it was a problem yet... -- Michael -- http://mail.python.org/mailman/listinfo/python-list