Re: need help on a data structure problem
Dongsheng Ruan wrote: Not quite related with Python. But my Data Structure course is experiemented on python and there is no data structure group, So I have to post here: Write a procedure (in pseudocode!) to increase the number of buckets in a (closed) hash table. Analyze its time and space complexity. What is the question about this problem that you would like to have answered? Certainly you don't want us to actually give you the answers to your homework?!? Here's some Python code to get us started with the discussion: class HashTable: def __init__(self): self.buckets = [ [] ] @staticmethod def Hash(object): return 0 def InsertItem(self, item): self.buckets[Hash(item)].append(item) The more I think about it, the more I realize you could probably just cut'n'paste that code and that should suffice for your answer! Good luck in Computer Science! -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Non-blocking pipes during subprocess handling
I'm using subprocess to launch, well, sub-processes, but now I'm stumbling due to blocking I/O. Is there a way for me to know that there's data on a pipe, and possibly how much data is there so I can get it? Currently I'm doing this: process = subprocess.Popen( args, bufsize=1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def ProcessOutput(instream, outstream): text = instream.readline() if len(text) 0: print outstream, text, return True else: return False while process.poll() is None: ProcessOutput(process.stdout, sys.stdout) ProcessOutput(process.stderr, sys.stderr) # clean up everything to EOF once the process ends. somethingPrinted = True while somethingPrinted: somethingPrinted = ProcessOutput( process.stdout, sys.stdout) somethingPrinted |= ProcessOutput( process.stderr, sys.stderr) Unfortunately, stream.readline will block 'til it gets a line, and typically there won't be anything on the stderr stream. The reason for the redirections in the first place is that I'm launching this script as a subprocess from a GUI app that catches stdout and stderr and directs the output to the appropriate windows, but in some cases I don't actually want the output at all (I've removed that logic though since it needlessly complicates my example; suffice to say everything below the process = subprocess.Popen... line is enclosed in a try and then in an if block. The documentation on file.read() indicate that there's an option for non-blocking mode, but I'm stumped as to how to even look for how to enable and use that. thanks, -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: A python library to convert RTF into PDF ?
[EMAIL PROTECTED] wrote: First, pdflatex is too slow. Second, my templates are M$-Word doc files, and they cannot be easily converted to tex. I have tried to convert them to tex using OpenOffice, but the result is ugly as hell. Ok, have you tried using the PDF printers (I've used PDFfactory before, but there are a number of free ones on SourceForge that may be worth a shot), and just using MSWord via COM to print to that printer? (This line of answers merely because nobody seems to know of a directly-Python solution.) -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: list/dictionary as case statement ?
Hendrik van Rooyen wrote: It works well - and it is surprisingly fast too... And its easy if the opcodes are all say one byte, else you need an opcode length field too, and fancier parsing. Often (always?) RISC architectures' instruction+operand lengths are fixed to the word size of the machine. E.g. the MIPS 3000 and 4000 were 32 bits for every instruction, and PC was always a multiple of four. ...which means the parsing is pretty straight forward, but as you say, simulating the rest of the machine is the hard part. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: list/dictionary as case statement ?
Bjoern Schliessmann wrote: Intels aren't RISC, are they? Not the ones in PCs. The OP didn't specify the CPU that's being used, however. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: A python library to convert RTF into PDF ?
[EMAIL PROTECTED] wrote: So, what library can I use to convert from RTF to PDF ? GPL / BSD Libraries are welcome. If you could write to LaTeX files instead, you could then just use pdflatex that comes with all of the LaTeX distributions. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: DOS, UNIX and tabs
Peter Decker wrote: Maybe I'm also weird, but I use a variable-pitch font when programming in Python. So a tab equals some number of spaces really isn't useful to me. My setup is, tab equals this much space. A year ago I would have thought you were weird, but after reading a post by Ed Leafe, one of the creators of Dabo about using proportional fonts for readability, I thought I'd try it out, thinking that it was pretty wacky. Turns out that after a very brief adjustment period, I liked it! Yep, I had a similar experience although a bit more forced. The editor that I was using was configured out-of-the-box with variable-pitch, and I didn't want to bother figuring out how to change it for the quickie stuff I was writing, then eventually I found that it no longer bothered me... -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: DOS, UNIX and tabs
Lawrence D'Oliveiro wrote: I think there should be a single environment variable, perhaps called TABS, which specifies the tab settings across all relevant tools that work with text, including less and diff. So for example setting this as export TABS=4 will cause these tools to treat tabs as equivalent to stepping to the next multiple of four columns from the start of the line. Maybe I'm also weird, but I use a variable-pitch font when programming in Python. So a tab equals some number of spaces really isn't useful to me. My setup is, tab equals this much space. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: DOS, UNIX and tabs
Marc 'BlackJack' Rintsch wrote: Did you try to open your code files with another editor, which has a different length for tabulator chars? It would look quite ugly, I guess... Actually, no. Everyone can choose their own number of spaces-per-tab and it'll look right, as long as everyone uses a monospace font. You never tried that with tabs plus additional spaces to line up e.g. arguments that are broken across lines, right? You must not understand what they're talking about, because it works fine. The example is this: \ class Foo: \tdef Function(): \t\tAnotherFunctionThatTakesManyArguments(arg1, \t\t arg2, \t\t arg3) And there are a number of environments where you can't change the length of a tab like email or terminals where code will be displayed from time to time for example as diffs from a version control system. That's the point of doing it in this way with tabs to specify indent level and spaces to specify tabular alignment. Me, I could never get emacs's python stuff to work suitably so I just use a Dead Simple Editor (SciTE) in which I use tabs exclusively; continuation indents are always exactly one additional tab over the thing that's being continued. Perhaps interestingly, for development I have my editor set to show tabs as fairly short, but my diff program shows them as eight characters. I find that makes indentation changes easier to spot in the diffs. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Managing a queue of subprocesses?
cypher543 wrote: That was a very good answer, and it sure sounds like it would work. However, I failed at implementing it. :( My updated runQueue() function is: def runQueue(self): self.buildProcess = None count = 1 # current position in the queue while True: if self.buildProcess is None: self.execute(self.cmdQueue[count][2], self.cmdQueue[count][0], self.cmdQueue[count][1]) count = count + 1 else: # I'm not really sure what to put here I pretty sure I did all of that wrong. ;) Also, how exactly would I redirect stderr to another place? You're thinking too hard. ;) class Whatever: def __init__(self): self.process = None # and other stuff, probably. def runQueue(self): # check to see if no process has been started, or if # the most-recently-started one has finished. if not (self.process or (self.process.poll() is None)): if self.process: previousReturnCode = self.process.returncode if len(self.cmdQueue) 0: command = self.cmdQueue.pop(0) # pull off the first command. self.execute(command[2], command[0], command[1]) else: self.process = None ...then, to prevent your GUI from freezing, you need to just call this function in your idle handling. If you don't want to start all of your commands at once, you need to not start all your commands at once. ;) -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with small program
Paul Watson wrote: It is certainly possible to construct a set of denominations for which the algorithm occasionally chooses badly. For example, if you give it the set (40,35,10) and ask it to make change for 70, it will be suboptimal. Unless I am missing the point, the minimum number of coins from the set available will be chosen. Surely this homework is past due by now. [...] doit(70, (40,35,10)) 70.0 requires 4 coins in hash {40: 1, 10: 3} The point was that minimum number of coins in this case is actually two, but the provided algorithm yields four. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does Python never add itself to the Windows path?
vbgunz wrote: I don't understand what all the fuss is about. Add a single page to the installer and on it, have 3 radio buttons. I don't understand what the fuss is about, and would not give that recommendation based on my not understanding it! I have never ever needed or wanted to launch the python interactive interpreter from a command-line. IPython is what I want to use when I'm going interactive, and running scripts from the command-line works just fine with Windows's file associations. Some if not most python documentation assumes Python is on the path... Really? I must live in different places in the docs, but I can't recall encountering any such documentation. Anyhow, I don't get why it doesn't apply by default in some way on Windows even if at the least it could be a simple reminder or tip to do so. Users who want it in their paths are certainly capable of putting it there. I am in the camp that detests apps that automatically install tons of crap everywhere without prompts. Certainly, though, the suggestion that one pane in the installer offer to put it in the path would leave the default as it is today (don't edit PATH), though, right? Doesn't make a whole lot of sense to add a new option and default it to something completely different from the old behavior, does it? -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: I want to see all the variables
Steven D'Aprano wrote: What does the author of the original class know about *my* needs and requirements? The only thing that the original class author might know is that mucking with data marked private may well cause problems, and hiding it therefore prevents those problems. It may turn out that accessing his private attributes is exactly what I need to solve my problem. It may also turn out that you /think/ that's what you need to do, but actually need to do something different. E.g. it may well be possible to access the bytes on the disk that holds your database directly, and that may be easier than going through the db API, but doing so may also be highly prone to error. I'm with the Python philosophy on this one: post all the Don't Touch This warning signs you like, but if somebody really wants to access my private attributes, to do something I never even imagined, they should be allowed to. You are allowed to. Have you been following the thread? Your answer's been mentioned several times. At the same time, you should ponder very carefully the reasons why the original author deemed it important to make those attributes private in the first place. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: how to serve image files without disk use?
Ray Schumacher wrote: But, how can I avoid disk writes? wx's *.SaveFile() needs a string file name (no objects). I'm going to investigate PIL's im.save(), as it appears to allow file-objects. Take a look at the img2*.py files in wx.tools. They're sorta sketchy imo, but they do the trick. encode_bitmaps.py, with the wx demo app, is used to generate all of the bitmap data that's in Python files embedded into the app, and following the chain of logic from there might yield you something interesting. Mind, I haven't tried it myself, but it's where I'd start looking. Good luck, -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: A stupid question
luxnoctis wrote: It says exactly: The specified module could not be found. LoadLibrary(pythondll) failed Don't know if that helps at all. There's something installed on Compaq computers that uses the Python stuff too, but I never figured out what it was. I figured it may have been some of the preloaded games, 'cause when I restored a roommate's machine from the restore image the Python22 folder was among the things that appeared on the fresh machine. It was actually pretty exciting for me since I needed to write a little utility to do something on this machine, but it was slow as death and only had dialup and I really didn't want to deal with downloading and installing that stuff myself. ...of course your first problem is buying a machine with a ton of shovelware on it. Second problem is uninstalling stuff without knowing what it's for. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Starting a child process and getting its stdout?
cypher543 wrote: This has been driving me insane for the last hour or so. I have search everywhere, and nothing works. I am trying to use the subprocess module to run a program and get its output line by line. But, it always waits for the process to terminate and then return the output all at once. Right, you need to use subprocess.PIPE and polling of the process and the pipe to get it to do what you want. Hopefully this makes sense, I also hope it'll work on Linux but can't imagine why it wouldn't. Save this code to a file, name unimportant. Executing the file will fire the top clause of the if statement, the bit that runs the parent part. That starts the script again as a subprocess, which triggers the else clause. Hope it makes sense. Depending on your environment, you might need 'shell=True' in your Popen args. On Windows, that is required if you want to prevent a console window from popping up if you're running in a GUI app. -tom! -- import subprocess import sys import time if len(sys.argv) == 1: # no command line arg means, we're the parent process. print 'starting parent process.' myName = sys.argv[0] # launch the subprocess with an additional parameter, to trigger # else clause below. p = subprocess.Popen( ( 'python', '-u', myName, 'x' ), stdout=subprocess.PIPE ) while p.poll() == None: data = p.stdout.readline() if data: print data, print 'process ended with return code', p.returncode else: # assume the command-line arg means run the background process print 'starting subprocess' for i in range(10): time.sleep(.25) print i sys.exit(14) -- http://mail.python.org/mailman/listinfo/python-list
Re: Starting a child process and getting its stdout?
Tom Plunket wrote: while p.poll() == None: data = p.stdout.readline() if data: print data, If you change that print to something more decorated, like, print 'process said:', data, Then it might be more obvious where/how the print statements are getting routed. Keep in mind that readline() will return one line at a time, and that line will end with a newline, although the last line you get (at EOF) may not have one. again, good luck. Hope this helps, -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Starting a child process and getting its stdout?
cypher543 wrote: Thank you for the examples, but I have tried all of that before. Did you try my example specifically? No matter what I do, my program always hangs while it waits for the process to exit and then it prints all of the output at once. self.buildPID = subprocess.Popen([python, tobeforked.py], ... By default, python will execute in buffered mode if it's attached to a pipe. Start it with the '-u' command line option and you may be good to go. (`python -h` for more info on this, man pages may also discuss it.) -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: textwrap.dedent replaces tabs?
OKB (not okblacke) wrote: (You dedent common leading tabs, except if preceded by common leading spaces (?)). There cannot be common leading tabs if they are preceded by anything. If they were preceded by something, they wouldn't be leading. Right, but 'common leading whitespace' is a broader term but similarly unambiguous. spacetab != tab, but there are two tabs of common leading whitespace in '\t\t ' and '\t\t\t'. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Starting a child process and getting its stdout?
Gabriel Genellina wrote: Did you *actually* tried what Tom Plunket posted? Two tiny chars make a difference. The sad irony is that before taking off for vacation I was struggling at work with the same problem in some sense. I couldn't figure out why for some processes I got all of the output right away and for others it all got queued up 'til the process ended. Working out a simple example for this thread showed the light: my calling of my_process.stdout.read() was blocking 'til EOF. Oops. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Managing a queue of subprocesses?
cypher543 wrote: self.buildPID = subprocess.Popen(buildCmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) Instead of calling it self.buildPID, you might just call it self.buildProcess or something. It's actually a Popen object that gets returned. So yes you can do what you want: __init__ self.buildProcess to None. Then, in execute(), if (self.buildProcess is None) or (self.buildProcess.poll() is not None) start the next process. You've got to wait for one process to end before starting the next one, it's really that easy. So, don't just go ahead and fire them all instantly. Possibly what you want to do instead is have runQueue() do the check somehow that there's no active process running. What I would do, have runQueue() check to see if self.buildProcess is None. If it is None, fire the next process in the queue. If it isn't None, then check to see if it's ended. If it has ended, then set self.buildProcess to None. Next UI update the next step in the queue gets done. Mind that you'll have to modify the queue as you go, e.g. self.queue = self.queue[1:]. Finally, consider piping stderr separately, and direct its output to a different window in your GUI. You could even make that window pop open on demand, if errors occur. good luck, -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Easiest way to print from XP/DOS.
jim-on-linux wrote: When the client runs the utility program the output file is built but nothing prints and no messages appear. If the file has a '.txt' extension, you could try os.system'ing start filename, which'll make the file pop open with notepad (or whatever happens to be associated with TXT files), from which the user would need to press Ctrl-P to make it print. Is it possible that virus detector or some self.defense software is interacting? Quite. I run firewall software on my PC that alerts me when a program is trying to launch another program. The message that it gives is not entirely unlike the one you gave me. To diagnose further, you could have the victim send you a screenshot to see what's really going on. With Outlook, it's as easy as hitting the Print Screen button (when the message is visible) and pasting the clipboard into an email. Alternatively, they paste into MS Paint, save the bitmap somewhere, and mail that to you. Good luck, -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: getting a process's PID
eldorado wrote: g = os.popen(ps -ef | grep HUB | grep -v grep | awk '{ print $2 }') h = g.readlines() g.close() h ['87334\012'] h = h[:-1] h [] I understand you're probably set, but instead of using readlines() you could also do this: g = os h = g.read().split('\n') and then your 'h' list would not have newlines. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: DOS, UNIX and tabs
Steven D'Aprano wrote: I don't know what problems with tabs you are talking about. I never have problems with tabs. *Other people* who choose to use software that doesn't understand tabs have problems. I've spent a lot of time reading both sides of the tabs versus spaces argument, and I haven't found anything yet that explains why tabs are, in and of themselves, bad. Indeed. In fact, I came to the conclusion several years ago that tabs are in better for formatting code because then different people on the team can have their preferred tabstop width, be it 8, 4, or 2 spaces. Ironically, it has always seemed to me then that tabs are superior for python editing, since mixing tabs and spaces in an environment like this means that stuff won't run, whereas in C it'll still compile even if the code looks awful. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: textwrap.dedent replaces tabs?
Frederic Rentsch wrote: Your rules seem incomplete. Not my rules, the stated documentation for dedent. My understanding of them may not be equivalent to yours, however. What if common tabs remain after stripping common white space? What if we just go with, [r]emove any whitespace than can be uniformly removed from the left of every line in `text`. ? Does this never happen? Or can we hope it doesn't happen? Hope has no place in programming software that is to be used by others. To err on the side of caution I complete your rules and this is my (tested) attempt at expressing them pythonically. Inasmuch as my rules have been expressed via tests, the provided code fails four of the five tests provided. (I admit it does look awfully sevety-ish. Just a vulgar little function.) Seventys-ish is as much a statement about the lack of statement about how you actually tested it as it is that an implementation was made apparently without understanding of the requirements. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Installing python.org distro over ActivePython?
Hey gang- I just ran into the fabled Secure Sockets not enabled in ActivePython, and the ActiveState FAQ says I should just grab _ssl.pyd from somewhere, offering up the python.org distribution as a possible source. I'm on 2.4 at this time, and python.org has what appears to be a considerably newer release than ActiveState in the first place, so I was wondering if I could just install this entire package right over the ActiveState installation, and everything would Just Work? thanks, -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: textwrap.dedent replaces tabs?
Frederic Rentsch wrote: It this works, good for you. I can't say I understand your objective. (You dedent common leading tabs, except if preceded by common leading spaces (?)). I dedent common leading whitespace, and tabs aren't equivalent to spaces. E.g. if some text is indented exclusively with tabs, then the leading tabs are stripped appropriately. If some other text is indented with common leading spaces, those are stripped appropriately. If the text to be stripped has some lines starting with spaces and others starting with tabs, there are no /common/ leading whitespace characters, and thus nothing is stripped. Neither do I understand the existence of indentations made up of tabs mixed with spaces, but that is another topic. At one point it was a fairly common cry in the How To Indent Python discussions. Maybe that cry has faded. I have been wasting a lot of time with things of this nature coding away before forming a clear conception in my mind of what my code was supposed to accomplish. Sounds stupid. Doesn't sound stupid, but there are in fact some fairly straight forward methods that can be put in place to alleviate that problem. The encounter with the devil in the details can be put off but not avoided. Best to get it over with from the start and write an exhaustive formal description of the problem. Follows an exhaustive formal description of the rules for its solution. Good lord, that's an amazingly 1970s way to look at programming! Modern software engineering practices have in some ways made these problems go away. In other words, coding should be the translation of a logical system into a language a machine understands. It should not be the construction of the logical system. This, anyway, is the conclusion I have arrived at, to my advantage I believe. To each their own, eh? I've been doing this a long time and have found that it is by far superior (for me) to refine the logical system as it is being implemented, as long as the business rules are encoded in such a way as to disallow the programmer from straying beyond them. My unit tests are far from exhaustive, but with code this simple it didn't seem terribly important since I was doing it more as a proof of concept, proving that I could do this sort of thing in not-many-more- lines-than-the-original-code-that-does-not-operate-to-its-published- specification. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: textwrap.dedent replaces tabs?
Frederic Rentsch wrote: Following a call to dedent () it shouldn't be hard to translate leading groups of so many spaces back to tabs. Sure, but the point is more that I don't think it's valid to change to tabs in the first place. E.g.: input = ' ' + '\t' + 'hello\n' + '\t' + 'world' output = textwrap.dedent(input) will yield all of the leading whitespace stripped, which IMHO is a violation of its stated function. In this case, nothing should be stripped, because the leading whitespace in these two lines does not /actually/ match. Sure, it visually matches, but that's not the point (although I can understand that that's a point of contention in the interpreter anyway, I would have no problem with it not accepting 1 tab = 8 spaces for indentation... But that's another holy war. If I understand your problem, you want to restore the dedented line to its original composition if spaces and tabs are mixed and this doesn't work because the information doesn't survive dedent (). Sure, although would there be a case to be made to simply not strip the tabs in the first place? Like this, keeping current functionality and everything... (although I would think if someone wanted tabs expanded, they'd call expandtabs on the input before calling the function!): def dedent(text, expand_tabs=True): dedent(text : string, expand_tabs : bool) - string Remove any whitespace than can be uniformly removed from the left of every line in `text`, optionally expanding tabs before altering the text. This can be used e.g. to make triple-quoted strings line up with the left edge of screen/whatever, while still presenting it in the source code in indented form. For example: def test(): # end first line with \ to avoid the empty line! s = '''\ hello \t world ''' print repr(s) # prints ' hello\n\t world\n' print repr(dedent(s)) # prints ' hello\n\t world\n' if expand_tabs: text = text.expandtabs() lines = text.split('\n') margin = None for line in lines: if margin is None: content = line.lstrip() if not content: continue indent = len(line) - len(content) margin = line[:indent] elif not line.startswith(margin): if len(line) len(margin): content = line.lstrip() if not content: continue while not line.startswith(margin): margin = margin[:-1] if margin is not None and len(margin) 0: margin = len(margin) for i in range(len(lines)): lines[i] = lines[i][margin:] return '\n'.join(lines) import unittest class DedentTest(unittest.TestCase): def testBasicWithSpaces(self): input = \n Hello\n World expected = \nHello\n World self.failUnlessEqual(expected, dedent(input)) def testBasicWithTabLeadersSpacesInside(self): input = \n\tHello\n\t World expected = \nHello\n World self.failUnlessEqual(expected, dedent(input, False)) def testAllTabs(self): input = \t\tHello\n\tWorld expected = \tHello\nWorld self.failUnlessEqual(expected, dedent(input, False)) def testFirstLineNotIndented(self): input = Hello\n\tWorld expected = input self.failUnlessEqual(expected, dedent(input, False)) def testMixedTabsAndSpaces(self): input = \t Hello\n \tWorld expected = \t Hello\n \tWorld self.failUnlessEqual(expected, dedent(input, False)) if __name__ == '__main__': unittest.main() -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: textwrap.dedent replaces tabs?
Peter Otten wrote: I guess I could manually replace all tabs with eight spaces (as opposed to 'correct' tab stops), and then replace them when done, but it's probably just as easy to write a non-destructive dedent. You mean, as easy as \talpha\tbeta\t.expandtabs() 'alpha beta' ? Umm, no, that's why I wrote eight spaces (as opposed to 'correct' tab stops). In either case, though, it'd be hard to know how to replace the tabs, so it'd be better not to remove them in the first place. Indeed, dedent() would work perfectly for my cases if it simply didn't do the expandtabs() in the first place, but there'd be some pretty glaring holes in its logic then. I've since written up a fairly reasonable (IMO) replacement though, that started with textwrap.dedent(), removed the expandtabs() call, and Does The Right Thing with tabs vs. spaces (e.g. it doesn't treat a tab at the beginning of a line as the same as eight spaces). -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: textwrap.dedent replaces tabs?
Frederic Rentsch wrote: Well, there is that small problem that there are leading tabs that I want stripped. I guess I could manually replace all tabs with eight spaces (as opposed to 'correct' tab stops), and then replace them when done, but it's probably just as easy to write a non-destructive dedent. This should do the trick: Dedent = re.compile ('^\s+') for line in lines: print Dedent.sub ('', line) The fact that this doesn't do what dedent() does makes it not useful. Stripping all leading spaces from text is as easy as calling lstrip() on each line: text = '\n'.join([line.lstrip() for line in text.split('\n')]) alas, that isn't what I am looking for, nor is that what textwrap.dedent() is intended to do. -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
textwrap.dedent replaces tabs?
The documentation for dedent says, Remove any whitespace than can be uniformly removed from the left of every line in `text`, yet I'm finding that it's also modifying the '\t' characters, which is highly undesirable in my application. Is there any way to stop it from doing this, or alternatively, to put those tabs back in? I see that TextWrap and its member functions take an 'expand_tabs' kwarg, but dedent unfortunately does not. ...I suppose such a function (replacement dedent) isn't terribly tough to write, but it seems an odd default especially considering there's no way to turn off the undesired changes, but were the changes /not/ made, the same text could just be passed through TextWrapper and have them removed... thx, -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: textwrap.dedent replaces tabs?
CakeProphet wrote: Hmmm... a quick fix might be to temporarily replace all tab characters with another, relatively unused control character. MyString = MyString.replace(\t, chr(1)) MyString = textwrap.dedent(MyString) MyString = MyString.replace(chr(1), \t) Of course... this isn't exactly safe, but it's not going to be fatal, if it does mess something up. As long as you don't expect receiving any ASCII 1 characters. Well, there is that small problem that there are leading tabs that I want stripped. I guess I could manually replace all tabs with eight spaces (as opposed to 'correct' tab stops), and then replace them when done, but it's probably just as easy to write a non-destructive dedent. It's not that I don't understand /why/ it does it; indeed I'm sure it does this so you can mix tabs and spaces in Python source. Why anyone would intentionally do that, though, I'm not sure. ;) -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Problem understanding how closures work
...at least, I think that I'm having a problem understanding the way closures work. I'm trying to define a function for an object which will take certain objects from the parent scope at the time that function is defined. For some reason, if I do this function definition in a loop, the locals given by that function (is this a closure?) are changed each iteration of the loop, whereas if the function definition is isn't looped over, I get the behavior I desire. Can anyone provide any insight for me? thanks, -tom! First, the output: Test 1 doesn't work the way I would expect: Test 4 says, Test 0 Test 4 says, Test 1 Test 4 says, Test 2 Test 4 says, Test 3 Test 4 says, Test 4 ...but test 2 does? Test 0 says, Test 0 Test 1 says, Test 1 Test 2 says, Test 2 Test 3 says, Test 3 Test 4 says, Test 4 Next, the program: class Test: def __init__(self, name): self.name = name def DoCall(self): self.ExternalCall(self.name) # The first test. def CreateTests1(count): tests = [] for i in xrange(count): name = 'Test %d' % i t = Test(name) tests.append(t) def ExCall(text): print '%s says, %s' % (name, text) t.ExternalCall = ExCall return tests # The second test. def CreateTests2(count): tests = [] for i in xrange(count): t = CreateTest(i) tests.append(t) return tests def CreateTest(index): name = 'Test %d' % index t = Test(name) def ExCall(text): print '%s says, %s' % (name, text) t.ExternalCall = ExCall return t print 'Test 1 doesn\'t work the way I would expect:' for t in CreateTests1(5): t.DoCall() print '\n...but test 2 does?' for t in CreateTests2(5): t.DoCall() -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem understanding how closures work
Rob Williscroft wrote: name in the above code is bound to a an entry in CreateTests1's locals, and ExCall has a (hidden) reference to that locals, so by the time ExCall is finally called the value associated with name has been replaced by (count - 1). Ah, I got it. Thanks. Thanks too to Gabriel. -tom! -- http://mail.python.org/mailman/listinfo/python-list
type(foo) == function ?
I'd like to figure out if a given parameter is a function or not. E.g. type(1) type 'int' type(1) == int True implies: def foo(): ... pass ... type(foo) type 'function' type(foo) == function Traceback (most recent call last): File stdin, line 1, in ? NameError: name 'function' is not defined Is there a way I can know if 'foo' is a function? thanks, -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: type(foo) == function ?
Erik Max Francis wrote: In dynamically-typed languages in general, explicit typechecks are not a good idea, since they often preclude user-defined objects from being used. Instead, try performing the call and catch the resulting TypeError: Good point, although I need to figure out if the thing can be called without calling it, so I can build an appropriate UI. Basically I expect three types of things in the 'value' field of the top-level dictionary. The three sorts of things that I will deal with in the UI are callable things (e.g. functions, for which Chris Mellon reminds me about callable()), mappings (e.g. dictionaries, used similarly to the top-level one), and sequences of strings. So I think callable() works for me in the general case, but now trawling the documentation in that area I'm not sure how I can tell if something is a mapping or if it's a sequence. The gist of the UI generation may be envisioned as: key is the name that gets assigned to the control. value indicates that the UI element is a: group box if the value is a mapping series of radio buttons if the value is a sequence of strings check box if the value is a function ...I've still gotta figure out the exact API, this is for a plugin sort of system that'll be used by the manually-driven version of the build process and this data is explicitly to build the UI for the various tools that are available. thanks, -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: Programmatically replacing an API for another module
Chris Lambacher wrote: I'm just a bit loathe to download and install more stuff when something simpler appears to be near-at-hand. ...especially considering the page describing this module doesn't offer any download links! http://python-mock.sourceforge.net/ Oh yeah, and the download link for python-mock is: http://sourceforge.net/project/showfiles.php?group_id=138804 Heh, yeah I'm sure that's what I got when I went to sf.net and typed in the project name, then submitted a bug for the homepage that it doesn't offer any links to find the download. ;) -tom! -- http://mail.python.org/mailman/listinfo/python-list
wx.Process.Kill on Win32
Google indicates that wx.Process.Kill hadn't been implemented some time ago in wxPython for Win32. Is that still the case? It's kind of a drag since several sources (including wxPython wiki) strongly advise (by my reading and intended use) using wxProcess over the built-in stuff, but it's not entirely useful if I can't kill long-running processes from an external driver. To tie into my previous question about mocking in Python, I've got a bunch of scripts that do a bunch of things, and there's one 'master' script which can serialize these things (connecting to databases, downloading lots of data, and applying transforms to that data). I realized that the GUI part could essentially just be a command line generator for the serializer script, and trivially launch the other activities as a separate process. I/O works pretty well and I was happy with it all until I tried to implement the 'Cancel' button. Any more info, now several years after those threads I turned up? thx, -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: Win32 COM
Tom Plunket wrote: I don't know anything about COM beyond the fact that I can look in the OLE/COM browser to maybe figure out the API that an object has that I need to operate on. I'm still not entirely sure what's going on, because there are some methods and properties that are available before doing this, but makepy was part of my solution. What I wanted to avoid was explicitly going around and running makepy N times on everyone's machine that might need to run this toolset. What I discovered I could do was run makepy with a -i command-line parameter, which would emit code that would kick the gencache to implicitly run makepy if necessary. So, problem solved! in my case: : C:\Python24\Lib\site-packages\win32com\clientmakepy -i : NxNXMLHelper 1.0 Type Library : {5E7B0F01-92C1-11D3-A5FB-00104B45886F}, lcid=0, major=1, minor=0 : # Use these commands in Python code to auto generate .py support : from win32com.client import gencache : gencache.EnsureModule('{5E7B0F01-92C1-11D3-A5FB-00104B45886F}', 0, 1, 0) Very handy! -tom! -- http://mail.python.org/mailman/listinfo/python-list
Win32 COM
I don't know anything about COM beyond the fact that I can look in the OLE/COM browser to maybe figure out the API that an object has that I need to operate on. Usually this gets me by fine; I've written mostly utilities for Developer Studio which work fine if a little slow, and a bit of junk for some other bits of the MS Office suite. However, now I'm a bit stuck. I've got this object, and the documentation and sample code imply that I can just call this method I need to call on it, and ding-dong. It's for creating a command object to pass to the application, and it works (or rather, is supposed to work) sorta like this: operation = win32com.client.Dispatch('AppHelper.AppParameter') operation.Command = 'A_Command_Name' operation.SetParameter('param1', 'value') However, I get an attribute error on the SetParameter call, which indicates that it doesn't exist. So, I look at the object browser, and sure enough it isn't there. However, there is a 'Parameter' method. Oh wait, there are two. There's one that takes the parameter name and (presumably) returns the value, and there's another that takes the parameter name and the value that it's set to. Through fiddling with this thing in IPython, I found that I can do: operation.Parameter('foo') ...which throws an exception, but once that's been run, I can do what I set out to do: operation.SetParameter('param1', 'value') ...and all is well. Can anyone give me any tips as to what's going on here and what I might be doing wrong? I have /not/ run the makepy.py or whatever it is, since when I roll this out to the team I don't want to have to run that on every machine before it'll work, but if that's what I need to do, well, so be it. However, at this time I'm stumped as to what to try except that I can have a dirty-feeling try/except bit to enable that call... thx, -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: CSV module, DictReader problem (bug?)
John Machin wrote: If you were to write 'c:\temp\book1.csv', it would blow up ... because \t - tab and \b - backspace. Get into the habit of *always* using raw strings r'C:\Temp\Book1.csv' for Windows file names (and re patterns). You could use double backslashing 'C:\\Temp\\Book1.csv' but it's uglier. ...alternatively you can just use 'unix slashes', e.g. 'c:/temp/book1.csv', since those work just fine 'cause the Windows APIs deal with them properly. -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding Worksheets to an Excel Workbook
wesley chun wrote: welcome to Python!! i too, have (recently) been interested in COM programming, so much so that i added some material... from time import sleep import win32com.client as win32 def excel(): xl = win32.gencache.EnsureDispatch('Excel.Application') ss = xl.Workbooks.Add() # add a new spreadsheet/workbook sh = ss.ActiveSheet # grab the active sheet of the workbook xl.Visible = True# make Excel show up on the desktop sleep(1) sh.Cells(1,1).Value = 'Python-to-Excel Demo' sleep(1) for i in range(3, 8): sh.Cells(i,1).Value = 'Line %d' % i sleep(1) sh.Cells(i+2,1).Value = Th-th-th-that's all folks! sleep(5) ss.Close(False) # close the workbook and don't save xl.Application.Quit() # quit Excel You've got a lot of sleep calls in there- did you find that things behaved erratically without them? I haven't done any Office automation with Python, but my DevStudio stuff has always worked a treat without the sleep calls. -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: Is a list static when it's a class member?
Fredrik Lundh wrote: so what's the practical difference between def __init__(self, name): self.name = name self.data = [] and def __init__(self, name): self.name = name self.data=[] Ignoring nerd-extreme-pedantic-mode for this circumstance, you elided the bits that were functionally different. IOW, based on the OP's post, it appeared that C++ was infecting their Python, and removing the class attributes entirely was likely what the OP actually wanted. In Python, you don't define the instance members in the class scope like the OP has done: the OP's approach works perfectly fine, as long as you understand that class attributes are shared. Obviously, as is sticking a gun in your mouth is perfectly fine, as long as you understand that pulling the trigger will yield a large hole in the back of your skull. My reading of the OP's post was that shared attributes were not desired. -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: C++ to Python?
Roman Yakovenko wrote: you want to find some kind of translator C++ code: std::cout 1; translator output: print 1 Am I right? If so, I am pretty sure that such translator does not exist - too complex. ...however such a refactor is easy for a human to do. What the OP could do is read up on wrapping the C++ API, and then do the change language refactor incrementally per source file. Even for a large application, if you had the whole C++ API wrapped so it was accessible via Python, it'd be fairly straight-forward and perhaps even easy. Heck, you could even write a quickie conversion utility that converted the top-level C++ syntax into Python syntax to get 90% of the way there. Even a quickie: def ConvertBraces(filename): lines = file(filename).readlines() indent = 0 for line in lines: stripped = line.strip() if stripped == '{': indent += 1 print ':', elif stripped == '}': indent -= 1 print '\n', '\t'*indent, 'pass\n' elif: print '\n', '\t'*indent, stripped files = glob.glob('*.cpp') for f in files: ConvertBraces(f) ...that may well get you started. ;) For fancy stuff like comment matching I'll suggest the re library. -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: Is a list static when it's a class member?
Fredrik Lundh wrote: if you want separate instances to use separate objects, make sure you create new objects for each new instance. see Tim's reply for how to do that. kath's response is probably better. In Python, you don't define the instance members in the class scope like the OP has done: class A: name = data = [] You define them when you attach them to an instance, e.g. class A: def __init__(self): self.member1 = 'a' def ThisWorksToo(self): self.member2 = 'b' # this also works a = A() a.member3 = 'c' -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode strings, struct, and files
John Machin wrote: message = unicode('Hello, world') myFile.write(message) results in 'message' being converted back to a string before being written. Is the way to do this to do something hideous like this: for c in message: myFile.write(struct.pack('H', ord(unicode(c I'd suggest UTF-encoding it as a string, using the encoding that matches whatever wchar means on the target machine, for example assuming bigendian and sizeof(wchar) == 2: Ahh, this is the info that my trawling through the documentation didn't let me find! Thanks a bunch. utf_line1 = unicode_line1.encode('utf_16_be') etc struct.pack(.64s64s, .., utf_line1, utf_line2) Presumes (1) you have already checked that you don't have more than 32 characters in each line (2) padding with unichr(0) is acceptable. This works frighteningly well. ;) -tom! -- http://mail.python.org/mailman/listinfo/python-list
Unicode strings, struct, and files
I am building a file with the help of the struct module. I would like to be able to put Unicode strings into this file, but I'm not sure how to do it. The format I'm trying to write is basically this C structure: struct MyFile { int magic; int flags; short otherFlags; char pad[22]; wchar_t line1[32]; wchar_t line2[32]; // ... other data which is easy. :) }; (I'm writing data on a PC to be read on a big-endian machine.) So I can write the four leading members with the output of struct.pack('IIH22x', magic, flags, otherFlags). Unfortunately I can't figure out how to write the unicode strings, since: message = unicode('Hello, world') myFile.write(message) results in 'message' being converted back to a string before being written. Is the way to do this to do something hideous like this: for c in message: myFile.write(struct.pack('H', ord(unicode(c ? Thanks from a unicode n00b, -tom! -- http://mail.python.org/mailman/listinfo/python-list
Deferred imports
I'm using this package that I can't import on startup, instead needing to wait until some initialization takes place so I can set other things up so that I can subsequently import the package and have the startup needs of that package met. Specifically this has to do with the interaction between wxPython and Pygame. (see http://wiki.wxpython.org/index.cgi/IntegratingPyGame, toward the bottom, after the On Windows however... heading for my writeup about the problem.) So as y'all might guess, I have the solution in this sort of thing: import os global myDeferredModule class MyClass: def __init__(self): os.environ['MY_DEFERRED_MODULE_PARAM'] = str(self) global myDeferredModule import myDeferredModule m = MyClass() HOWEVER, my problem now comes from the fact that other modules need to use this import. So, I need to either use the above trick, or I need to just import the module in every function that needs it (which will be almost exclusively the constructors for the objects as they're the ones creating Pygame objects). So my question is, is 'import' heavy enough to want to avoid doing it every time an object is created, or is it fairly light if it's currently active somewhere else in the application? I suppose that may depend on how many objects I'm creating, and how frequently I'm creating them, but if 'import' resolves to essentially if not global_imports.has_key(module): do_stuff_to_import_module ...then I'm not so worried about putting it into every constructor. Otherwise I'll do this trick, starting myDeferredModule = None and only do the import if not None. Thanks! -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: String handling and the percent operator
Justin Azoff wrote: Of course.. I should read the python documentation at http://docs.python.org/lib/typesseq-strings.html Excellent. Thanks. Has this been around long? I learned Python in the 1.6 days iirc, but haven't done much except simple scripting with it since... -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: String handling and the percent operator
Erik Max Francis wrote: For enrichment purposes, is there a way to do this sort of thing with a generator? E.g. something like: def SentenceGenerator(): words = ['I', 'have', 'been', 'to', 'the', 'fair'] for w in words: yield w message = %s %s %s %s print message % SentenceGenerator() (I ask because the above doesn't work)? Use tuple(SentenceGenerator()). A generator is just another object, so using it with the % operator tries to substitute it at one value. (Even with this fix, though, your message didn't have enough formatters.) I know that the message didn't have enough formatters, that's why I asked. (Although I would have assumed that the generator would get automatically converted to a sequence that was consumable by the interpolation operator...) When using a generator via .next(), there's no requirement that it is used until it is exhausted. E.g. def NewId(): v = 0 while 1: yield v v += 1 NextId = NewId().next BUTTON_OK = NextId() BUTTON_CANCEL = NextId() BUTTON_YAY = NextId() Hence my question being something like rather than something equivalent to. Alas, little did I know that the answer I was looking for was not even up the same path. -tom! -- http://mail.python.org/mailman/listinfo/python-list
String handling and the percent operator
I have some code to autogenerate some boilerplate code so that I don't need to do the tedious setup stuff when I want to create a new module. So, my script prompts the user for the module name, then opens two files and those files each get the contents of one of these functions: def GetPyContents(module): boilerplate = \ class %s: pass if __name__ == '__main__': import unittest unittest.main('%s_t') return boilerplate % ((module,) * 2) def GetTestContents(module): boilerplate = \ from %s import * import unittest class Test%s(unittest.TestCase): def testConstruction(self): self.failUnless(%s()) def testWriteMoreTests(self): self.fail('This test should fail.') if __name__ == '__main__': unittest.main() return boilerplate % ((module,) * 3) My question is, I don't like hardcoding the number of times that the module name should be repeated in the two return functions. Is there an straight forward (inline-appropriate) way to count the number of '%s'es in the 'boilerplate' strings? ...or maybe a different and more Pythonic way to do this? (Maybe I could somehow use generators?) thx. -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: String handling and the percent operator
Simon Forman wrote: strings have a count() method. thanks! For enrichment purposes, is there a way to do this sort of thing with a generator? E.g. something like: def SentenceGenerator(): words = ['I', 'have', 'been', 'to', 'the', 'fair'] for w in words: yield w message = %s %s %s %s print message % SentenceGenerator() (I ask because the above doesn't work)? -tom! -- http://mail.python.org/mailman/listinfo/python-list
Re: to py or not to py ?
Carl J. Van Arsdall wrote: Because of the GIL only one thread can actually run at a time. I've recently been wondering about this, since in the work I do, a lot of time is spent doing disk I/O. So if I want the UI to remain responsive, I could spawn an IO thread to handle requests, and do a pretty simple just whack new requests onto the queue without locks since I'm guaranteed to not have the IO thread read at the same time as the requestor thread? ...what exactly constitutes an atomic operation in Python, anyway? e.g. class IoThread: # ... # called from the other thread... def RequestFile(self, name): self.fileQueue.append(name) # called during the IO thread def GetNextFile(self); next = self.fileQueue[0] self.fileQueue.pop(0) return next ? -tom! -- http://mail.python.org/mailman/listinfo/python-list