Binary representation of floating point numbers
Hi, I'm using python to run some lab equipment using PyVisa. When I read a list of values from the equipment, one of the fields is 32 bits of flags, but the value is returned as a floating point number, either in ASCII format, or pure binary. In either case, since I'm using PyVisa, it converts the number to a single precision floating point, and that's what I have to work with. The question is how do I recover the bits out of this floating point value so I can read the flags represented here? Also, it's little (or big?) endian. Whatever... how do I manipulate the endianness? thanks Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Binary representation of floating point numbers
Hi, okay, let me be more concise. The lab equipment has two formatting modes, ascii, and float. In ascii mode, it returns strings that represent the numeric value, so e.g. 3.14 is returned as '3.14'. PyVisa, when set to read ascii mode, will convert these strings to float with "visa.read_values()". (easy enough to do myself too with visa.read(), split(), and eval()). In float mode, the instrument returns a sequence of bits that are exactly the ieee754 number in the case of floats, or just the flags in the case of flags. PyVisa, when set to float mode, will convert everything to float, because it is unaware apriori that one of the fields returned is actually intended to be used as binary flags. Secondarily, I had to set the instrument to return the bits in small endian for it to read properly (I could aternately set PyVisa to swap endianness for me). I may need to manipulate this. Actually now that I read the very confusing manual, it looks like maybe the flags is returned as a decimal number, but it's not clear how this is returned in either ascii or float mode. In any case, I think I will need to manipulate "native" numbers into binary representation. Looks like I should figure out the struct module... Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Binary representation of floating point numbers
Ok, I figured it out... The only way to get the flags is as a float, either through an ascii string or a true float. The value of the float, however, is representable as 24 bits of normal binary. So for example, the value returned is +4.608400E+04 which is really an int, 46084, which is more easily convertible to binary. So the question becomes how to convert an int to binary, which I found here http://groups.google.com/group/comp.lang.python/browse_frm/thread/300e220394e2d841/33bc9b0d8174b038?lnk=st&q=python+int+to+binary&rnum=1#33bc9b0d8174b038 So problem solved (just need to implement it). Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Binary representation of floating point numbers
Actually that's probably the easiest way. I may want to use shorter variable names :) thanks michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Binary representation of floating point numbers
That looks pretty cool. I'll try it out. thanks Michael -- http://mail.python.org/mailman/listinfo/python-list
Moving a package in cygwin
Hi, I'm using python 2.4 and windows XP. I have two packages in the windows version of python in site-packages. They are PyVisa and ctypes, and both live in c:\python24\lib\site-packages I'd like to move these to the cygwin version of python on the same system. I tried copying the PyVisa and ctypes directorices (including other miscellaneous "junk" like .pyd files associated with ctypes) to c:\cygwin\lib\python24\site-packages\, but that didn't do the trick. It appears site-packages *is* in my path. Aside from the pyvisa and ctypes directories, site-packages also contains: _ctypes.pyd, _ctypes_test.pyd, visa.py, visa.pyc, visa.pyo. For the windows version, this appears to be enough. For cygwin, however, it's not. This is what I get under cygwin: $ python Python 2.4.1 (#1, May 27 2005, 18:02:40) [GCC 3.3.3 (cygwin special)] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> import visa Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.4/site-packages/visa.py", line 1, in ? from pyvisa.visa import * File "/usr/lib/python2.4/site-packages/pyvisa/__init__.py", line 27, in ? import vpp43 File "/usr/lib/python2.4/site-packages/pyvisa/vpp43.py", line 41, in ? from vpp43_types import * File "/usr/lib/python2.4/site-packages/pyvisa/vpp43_types.py", line 38, in ? import ctypes as _ctypes File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 13, in ? from _ctypes import Union, Structure, Array ImportError: No module named _ctypes >>> The .pyd files are the only files with "_ctypes" in the name. There are no references to pyvisa or ctypes in the c:\python24\lib directory. All the documentation points to adding an arbitrary path via modifying site.py, sys.path, or other tricks, or installing fresh. I tried installing ctypes with source (python setup.py build...) but that lead to compile problems which I won't go into here. It seems like moving an already-working package should be easy. Is there anything I'm missing? Seems like it should be a one-liner someplace. thanks Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving a package in cygwin
Ok, thanks. I actually hadn't considered the build system, figuring that function names, etc., would somehow magically be "exported" to anyone wanting to use the .pyd library. Michael -- http://mail.python.org/mailman/listinfo/python-list
Fancy GUI with Python
Hi all. I just downloaded and installed the new Office suite from MS with their new 'ribbon' based UI. I think it's pretty cool and AFT* for a new UI paradigm. I hope it sticks. Anyway, I'm wondering how to implement a gui like this with Python. I don't think wx or qt or gtk or tkinter support this sort of fading and glowing type of effects... or do they? I played with wx way back in 2000 or so (C++ version), and it certainly didn't have any of that. I don't know if this stuff is now built into XP, or if it's specialized libraries only accessible to MS for their purposes. Can a python gui framework be redirected to use the new gui? Or is this something that has to be manually emulated from a low-level if python is to make use of it? What about under linux? So I'm not sure if this is a Python question, a xxx-Python question (where xxx is the widget toolkit of choice), or a windows API type of question. How does one make fancy fading guis with python? (cross-platform if possible) thanks ms *AFT = about freakin' time -- http://mail.python.org/mailman/listinfo/python-list
Storing nothing in a dictionary and passing it to a function
Hi, I'm writing a hand-written recursive decent parser for SPICE syntax parsing. In one case I have one function that handles a bunch of similar cases (you pass the name and the number of tokens you're looking for). In another case I have a function that handles a different set of tokens and so it can't use the same arguments as the first one, and in fact takes no arguments. However, these functions are semantically similar and are called from the same place one right after the other. I'd like to have a dictionary (actually a nested dictionary) to call these functions so I can avoid if-then-elsing everything. Eath dictionary item has three things in it: the function to be called, a string to pass to the function (which is also the key to the dict), and a tuple to pass to the function. In the case of the function with no arguments, obviously I'd like not to pass anything. I'm trying to do this 'functionally' (i guess), by avoiding if-then-elses and just calling out the functions by accessing them and their arguments from the dictionary. something like this: alldict = \ {'pulse': {'func': self.arbtrandef, 'args':(2,5)},\ 'sin' : {'func': self.arbtrandef, 'args':(2,3)},\ 'exp' : {'func': self.arbtrandef, 'args':(2,4)},\ 'pwl' : {'func': self.pwldef, 'args': (None,)},\ <--- how do I store "no" arguments? 'sffm' : {'func': self.arbtrandef, 'args':(5,0)}} for it in alldict.items(): name = it[0] args = (name,) + it[1]['args'] it[1]['func'](*args) So basically this doesn't work. I am either trying to pass a tuple of (name, None,) to a function (pwldef) that doesn't take any arguments, or I'm trying to pass None itself, which also doesn't work. I could try changing pwldef to take 3 arguments and then just throw them away, but that's cheesy. It almost seems like in 'args' there should be a little function that creates an argument. I tried using a lambda in another situation to "do" something (an assignment) rather than "return" something, but it didn't work. Any thoughts? thanks ms -- http://mail.python.org/mailman/listinfo/python-list
Re: a good programming text editor (not IDE)
Istvan Albert wrote: > Scott David Daniels wrote: > > > To paraphrase someone else (their identity lost in my mental fog) about > > learning VI: > > "The two weeks you'll spend hating vi (or vim) as you learn it will > > be repaid in another month, ad the rest is pure profit." > > Time and again I hear this (no shortage of Vim fans, same with Emacs), > and I know I should know better but always believe them yet again. > Invariably I download Vim play with it for an hour, get increasingly > frustrated and give up. Most likely I'm greatly spoiled by using > EditPlus (Windows only), it just makes it so easy to do the basic > programming tasks that I need, everywhere else I turn I see far more > functionality but at the price of not being able to do basic tasks as > efficiently as I need them. > > Say I want to be able to execute the python program I'm currently > editing. Nothing simpler in EditPlus, Tools->Configure Tools->Add Tool > then specify which program you want to run, what parameters it takes > and whether to capture the output or not ... total time it took me > first time I wanted to do this ... about 3 minutes ... from now on > pressing Ctrl-1 while editing the source will execute the python on the > current source *and* it displays the output in a lower pane as it runs > *and* it allows me to simultanously edit the file *while* the program > is running. Outstanding. > > Yet after searching and reading stuff for more than an hour I was > unable to accomplish the same thing in Vim though I'm already familiar > enough with basic editing and setup (through my previous trials and > tribulations) ... I have a few solutions that end up doing something > similar but are quite a few keypresses longer both to invoke or to get > back to the source that I'm writing, or I lose editing control while > the program is running ... etc... > > So if the OP is on windows just head over and get EditPlus (UltraEdit > and TextPad seem to be similar), and just accept the fact that you are > already using an editor that as good as an editor can get ... I know > I'd pay that registration once again for an editor that works the same > way on Linux... > > i. I'm a huge EditPlus fan and there are not enough good things I can say about it. The main thing about GUIs which unix people don't seem to get is discoverability is key. Every time I've tried using x-emacs or x-anything (or even Scite as it seems to be popular), there is very little that is intuitive or obvious about the interface, and even though you have a GUI right there in front of you, things are still controlled in dot whatever files, with some special syntax and magic words for everything! Even doing a search and replace in x-emacs is f*cked up. I was trying to use it for something a few years ago (I dunno, maybe 2002 or 2003 on debian or 2000 on redhat), and it just didn't work the way I expected it to work, ie it didn't follow what has become the standard GUI (read: windows) way of doing things. There should be a dialog for search and replace. If it doesn't do it that way, then don't pretend to be a gui, because you're not, you're pandering to the "we want a gui crowd" while still stickin' it to 'em by forcing them to remember commands for shit like changing word wrapping (oh my god there's a *mode* for word wrapping? m-x-wtf change-the-mode-to-something-I-forget-what) and reminding everyone what a privilege it is to learn f***king lisp to enable some obscure little tweak which could just as easily have shown up in a checkbox. Scite was jacked because the main window didn't have anything discoverable on it, and one of the main menus just had a list of syntaxes you could use to highlight your code... and the other menu opened up your dot whatever file to change the settings. That's just retarded. I think there is a fine line between being too dumbed down to do anything (point click ooh aahh), and so "flexible", "customizable", and "free" that it feels you have to roll-your-own text editor each time you want to start a new project. This is the best article on guis and stuff I've read in a while. http://www.joelonsoftware.com/uibook/fog000249.html Anyway, the only thing editplus doesn't do that I wish it did is code folding. All the stuff you guys are talking about: line numbers, syntax highlighting, custom tools (running the interpreter), regexp search and replace, keeping your environment the same between sessions, soft word wrap, tab-vs-spaces, auto-indent, braces-matching, bla bla... it does it all in an appropriately gui manner without making you feel like a moron for not psychically knowing the command ahead of time, or for not having someone to copy a dot whatever file from, or not Reading TFM (which of course never tells you what you want to know anyway -- it's either a patronizingly simple-minded 3rd grade tutorial, or it tells you what a command does if you already know its f***king name!). And of course all these editp
Re: a good programming text editor (not IDE)
> > Cream is a package built on top of vim that presents a more "Windows > friendly" face to the vim/gvim editor. Cool thanks, I'll check it out. -- http://mail.python.org/mailman/listinfo/python-list
Interactive debugging
Hi, is there a way in python to place some sort of keyboard() type statement which stops the script and puts you back at the console? I'm looking for something like in matlab, where you place a keyboard() command (I think), then you're in debug mode in the console, and you type continue to re-enter the script where you left off. While you're in the debugger/console you're at the scope where the keyboard() call was and you can manipulate variables, etc. Is there something like this in python? I've been using winpdb, which is great, but sometimes I need more interactivity. Is this what pdb is about? thanks ms -- http://mail.python.org/mailman/listinfo/python-list
Self-identifying functions and macro-ish behavior
Hi, I was wondering how I may get a python function to know what its name is without me having to write it manually? For example: def func1(): print 'func1' return True def func2(): print 'func2' return True should be more like def func1(): print return True def func2(): print return True I imagine this means things like closures which I'm not familiar with (I'm not a CS person). In this case, each function is part of a class, so I imagine I can take a dir() of the class if necessary. This leads into my next related question, which is How do I get some sort of macro behavior so I don't have to write the same thing over and over again, but which is also not neatly rolled up into a function, such as combining the return statements with a printing of ? My application has a bunch of functions that must do different things, then print out their names, and then each call another function before returning. I'd like to have the last function call and the return in one statement, because if I forget to manually type it in, things get messed up. (ok, I'm writing a parser and I keep track of the call level with a tab count, which gets printed before any text messages. So each text message has a tab count in accordance with how far down the parser is. Each time a grammar rule is entered or returned from, the tab count goes up or down. If I mess up and forget to call tabsup() or tabsdn(), the printing gets messed up. There are a lot of simple cheesy production rules, [I'm doing this largely as an exercise for myself, which is why I'm doing this parsing manually], so it's error-prone and tedious to type tabsup() each time I enter a function, and tabsdn() each time I return from a function, which may be from several different flow branches.) Thanks for any help :) Michael -- http://mail.python.org/mailman/listinfo/python-list
Python vs. Lisp -- please explain
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely hated it, having learned C++ a few years prior. They didn't teach Lisp at all and instead expected us to learn on our own. I wasn't aware I had to uproot my thought process to "get" it and wound up feeling like a moron. In learning Python I've read more about Lisp than when I was actually trying to learn it, and it seems that the two languages have lots of similarities: http://www.norvig.com/python-lisp.html I'm wondering if someone can explain to me please what it is about Python that is so different from Lisp that it can't be compiled into something as fast as compiled Lisp? From this above website and others, I've learned that compiled Lisp can be nearly as fast as C/C++, so I don't understand why Python can't also eventually be as efficient? Is there some *specific* basic reason it's tough? Or is it that this type of problem in general is tough, and Lisp has 40+ years vs Python's ~15 years? Thanks Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Python vs. Lisp -- please explain
Cool, thank you. That's the answer I was looking for :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python vs. Lisp -- please explain
Great, thanks for a very complete answer. michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Python vs. Lisp -- please explain
Great, thank you and everyone for this nice discussion. Michael -- http://mail.python.org/mailman/listinfo/python-list
Why does stack.inspect take so long?
Hi, I've written a top-down recursive decent parser for SPICE circuit descriptions. For debugging purposes, I wanted each production rule/function to know what its own name was, so at the beginning of each rule/function, I make a call to inspect.stack()[0][3] (I think...) and that fetches the name from someplace. However, with a bunch of test text input, this takes ~10 seconds to run. When I comment out the inspect.stack() function from each production/function, the program speeds up to 0.04s, or 250x !! I loaded up the profiler to see what was there, and although I don't understand the output entirely, it looks like the stack frame functions are used in disproportionately large amounts compared to other often-used functions. This is a *recursive* parser, so it's being called a lot even for simple things. I'm wondering still why there is a 250x speed up when I comment it out. Any clues? thanks ms -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does stack.inspect take so long?
Tim Peters wrote: > [EMAIL PROTECTED] > > Hi, I've written a top-down recursive decent parser for SPICE circuit > > descriptions. For debugging purposes, I wanted each production ... > > Any clues? > > It should go much faster to use a function that doesn't crawl the > entire call stack. For example, > > >>> import sys, inspect > >>> def name_of_caller(): > ... return inspect.getframeinfo(sys._getframe(1), context=0)[2] > >>> def f(): > ... print "my name is", name_of_caller() > >>> f() > my name is f > > name_of_caller() takes time independent of the call-stack depth. > > The "context=0" is to avoid wasting time sucking up and packaging > source-code lines you don't want anyway. Cool, thanks. I'll try that. I didn't realize there were other ways of getting it. ms -- http://mail.python.org/mailman/listinfo/python-list
Using python for a CAD program
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what I *want* at the end of this; I just have never taken on a programming task of this magnitude. I've seen that some are using python as a utility language for existing CAD environments, and I've also found some guy who's writing a 2d drafting tool with python, which is cool. I've been playing with python recently and so far have a SPICE parser half-written (it'll do production rules, but no syntax trees yet...) I'm wondering if the python experts can provide some opinion on python's efficiency/utility in the following areas, and perhaps where some things will need to be in C++ and/or pyrex: 1. Databases. I don't mean sql type database, but design databases, which contain all the components for any given design, including schematics, layout, simulation parameters, etc. I'm not concerned about python's ability to do fancy data structures, but I'm wondering how it'll go in terms of efficiency when things get really big. If the alternative is home-brewed C++ linked-lists, attributes, arrays, binary trees, memory management, etc., it looks like python's ability to do all this neatly without all the bugs waiting to happen if I did this myself are well worth the efficiency loss that may result from its uber-generality. 2. GUI. Yes, I know you can do guis with qt, gtk, tkinter, etc. I'm talking of fancy guis that do alpha blending, animations, nice shading/gradients, etc. in a quick, smooth, and slick way, such that moving a scroll bar or jiggling the mouse yields fast game-like response time, and which gives this program the feeling that you're actually in the 21st century... ie this is an *interactive* environment, and I f***king hate crass-hatching, so real colors is a must. Can this be portable between linux and windows? Is it possible to "do the whole thing" in opengl, even the 2d stuff? I guess I dont know enough about guis here. My fear is that my app will turn into x-hell if I try to do more than 8-bit colors in linux, with flashing background windows and complaints from the server about bit planes. Maybe I need to play with linux a bit more from the development side before commenting on this, since I get the feeling I'm working on old information here... 3. Computational stuff. I imagine the really heavy computing needs to be done in c++, especially if I'm stupid enough to write my own simulator, which my brain seems to want to do (I'm imagining a small homer simpson brain with a mind of its own telling me what to do...). But what about other things like rules and constraints about layout, routing, compaction, undo/redo, etc? These aren't really computationally expensive, I don't think, but do munge about quite a bit with the database. Is it likely I'll write something faster in C++? (answer: no). 4. Programmability. I imagine this is a slam-dunk, relatively speaking, to have a python interpreter as part-and-parcel of the design system. But how do I customize the command window to do custom things like adding special shortcuts (eg '?' brings up help, or help on commands a la Matlab), or making an API into the CAD program? I'm not sure conceptually how to merge an embedded python interpreter into the rest of the CAD program in an intelligent way, so some stuff is exposed, but not all, and which interacts dynamically with the graphical portion so that, for instance, context-sensitive help is available in the console window, or so you can access information about the currently selected object, or so you can do stuff from the command line that is based on your current graphical context. 5. Threads and parallelism. One of the things about this is that I'd like there to be some real-time simulator running so when you change the values of parts, the output changes, and I'd like to be able to take advantage of mulitple processors, or dual-core, or whatever. I've seen fake multi-threading in python (read about it, but I haven't done it), but that doesn't really use any extra cycles from a separate processor. So why am I thinking about this? I've used a few "real" CAD systems, and from using them, it's obvious the modus is to kludge shit together over 20 years and screw the user experience. So basically I'd like to take over the world with my nifty new python-based cad system because it's so beautiful and fun to use, and so flexible that my time to market for any given new idea is 5x shorter than the existing tools. Any comments on the above from people who've actually written stuff would be greatly appreciated! :) thanks ms -- http://mail.python.org/mailman/listinfo/python-list
Re: Using python for a CAD program
Cool. thanks for the links. I've already looked around quite a bit, and am very hesitant to just write more shit on top of other shit. The idea behind this is it's completely mine. So yes, I have a tendency to want to reinvent a few wheels, but I think it'll give me greater satisfaction. The problem with geda, etc., is that it's taking the unix approach -- a buncha little command line tools that somehow make a "system", using text files as communication medium. I hate that, largely because it allows the infectious spread of little files all over your system, encourages people to write scripts from outside the system that probably won't work for you, and exposes the user unnecessarily to the implementation of where you keep files, directories, blabla. I'm more of the windows approach, where you have one integrated environment, and any text you type is from within the application itself, and most application-related data is hidden from the user unless he *really* wants to get at it. (disclosure: I've never actually installed geda, but I tried icarus once and couldn't get it to compile -- another bane of open source stuff I can't stand. I'm not a CS person, so when I download something, I just want it to work, and I don't get off trying to *make* it work...). Another reason for doing this on my own is that I'd like a general-purpose CAD/design framework, of which electrical/IC design is only one part. Also, I think geda simulations are spice-based, which is batch, which is sooo your-father's buick, which I hate as much as text files... aaand another thing is I'm very big on user-experience. I'd like my program to *look* slick, like it belongs in a movie or something. I think that means starting from scratch, since I've not seen any CAD program take any artistic/human/psychological approach to its design. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using python for a CAD program
Yes, I figured I should be pretty expert at what's out there first before redoing something and making in inferior to the existing solution. I took a quick peek at cadence courses, and they're out of my personal price range. I have a new job coming up which should lead into IC design after some time, and I should probably ask them to send me to the course. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using python for a CAD program
Hi, Actually, I've wondered about using a custom-rolled binary or text-based database, vs. something that uses the OpenEDA standard, vs a 'real' database like sql. I guess my impression is that something like SQL is not designed for something as multi-... uh, dimensional? as an EE design. Perhaps it would be good for just storing the data, but can it also store relationships or other meta-things besides the raw literalness of a design? I'd imagine it can store arbitrary binary data, but then what's the advantage over a custom binary file? If the application provides a nice API for geting into its custom binary file, then what's the problem? ms -- http://mail.python.org/mailman/listinfo/python-list
Re: Using python for a CAD program
Thanks for this. I'm enjoying this discussion and I'm learning a lot about people's views and how they differ from mine. However, I'm still wondering about my original post. Can the experts please comment on python's usage for the following: 1. Databases. Assuming I roll my own, does python have any performance issues for this sort of thing? 2. GUI. Can Python command the various gui libraries fast enough to qualify as game-quality, with transparency, anti-aliasing, animations, etc? 3. Computational stuff like simulations and rules-checking. I'm just going to assume this needs to be in C++ or pyrex or SciPy or something. 4. Programmability. How do I embed an interpreter which has some custom CLI commands and knowledge of the graphical conext and which has exposure to the API used in the main application, such that to the user all the necessary things are exposed by default, and so that it doesn't feel like it's just a disconnected console window that he/she needs to set up manually each time? 5. Threads and parallelism. Should I even bother? I've read that it's possibly more tricky with python than with normal dev tools. I've never done it, but I'd like to at least think about it up front so if/when this goes MT it's not a complete rewrite from the ground up. thanks ms -- http://mail.python.org/mailman/listinfo/python-list
Re: Tabs versus Spaces in Source Code
I use Edit Plus for all my text-editing needs. With a simple shift-alt-i it faintly displays all spaces as little dots and all tabs as '>>' (but using the single ascii character instead). I use tabs to indent blocks, then if stuff within a block needs to be aligned (such as if statements or dictionaries), I used spaces. So this is mixing tabs and spaces, but the indentation level is always tabs. -- http://mail.python.org/mailman/listinfo/python-list