Re: Looping using iterators with fractional values
drife wrote: Hello, Making the transition from Perl to Python, and have a question about constructing a loop that uses an iterator of type float. How does one do this in Python? Use a generator: >>> def iterfloat(start, stop, inc): ... f = start ... while f <= stop: ... yield f ... f += inc ... >>> for x in iterfloat(0.25, 2.25, 0.25): ... print '%9.2f' % x ... 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout
Ron Garret wrote: But this topic does bring up a legitimate question: I have a bunch of code that generates HTML using PRINT statements. I need to convert all this code to return strings rather than actually printing them (so I can use the results to populate templates). In Lisp I could do this: (with-output-to-string (s) (let ( (*standard-output* s) ) (call-html-generating-code) s)) Is there an equivalent Python trick to capture a function call's output as a string? Just to make sure I understand, I'm going to restate your question: Is there a way to capture stdout? The answer: Sure, replace it with something file-like: >>> import sys, StringIO >>> default = sys.stdout >>> writer = StringIO.StringIO() >>> sys.stdout = writer >>> print 'Whatever' >>> sys.stdout = default >>> print writer.getvalue() Whatever >>> // m -- http://mail.python.org/mailman/listinfo/python-list
Re: ? about file() and open()
Sean wrote: Was wondering if there was any difference between these two functions. None, as shown here: D:\Python23>python Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> file == open True >>> I have read some text that said file() wasn't introduced until 2.2 and that it was synonymous with open(). Does this mean that I should be using file() where I used open() before? Google is your friend: http://www.google.com/search?q=file+vs.+open+python http://mail.python.org/pipermail/python-dev/2004-July/045931.html // m -- http://mail.python.org/mailman/listinfo/python-list
Re: Building unique comma-delimited list?
Roy Smith wrote: You've got a list of words (actually, they're found by searching a data structure on the fly, but for now let's assume you've got them as a list). You need to create a comma-delimited list of these words. There might be duplicates in the original list, which you want to eliminate in the final list. You don't care what order they're in, except that there is a distinguised word which must come first if it appears at all. Some examples ("foo" is the distinguised word): ["foo"] => "foo" ["foo", "bar"] => "foo, bar" ["bar", "foo"] => "foo, bar" ["bar", "foo", "foo", "baz", "bar"] => "foo, bar, baz" or "foo, baz, bar" The best I've come up with is the following. Can anybody think of a simplier way? Who knows whether this is "simpler", but it does demonstrate that you can customize the sort of a list: #!/usr/bin/env python def makesorter(first): """Return a sort function that sorts first to the top.""" def sorter(x, y): if x == first: return -1 elif y == first: return 1 else: return 0 return sorter words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"] first = 'foo' sorter = makesorter(first) unique = {} for word in words: unique[word] = word keys = unique.keys() keys.sort(sorter) print ', '.join(keys) -- http://mail.python.org/mailman/listinfo/python-list
Re: printing line numbers for debugging purpose
Philippe C. Martin wrote: Hi, All of the methods from my program return None on error (i.e; I do not want to assert and have the program exit). Is it possible to print the current source file name/line number ? ex: in C/C++ I would use the macros __FILE__ and __LINE__. Consider something like this: #!/usr/bin/env python def main(): raise RuntimeError('whatever') if __name__ == '__main__': try: main() except: import sys, traceback t, v, tb = sys.exc_info() traceback.print_tb(tb) -- http://mail.python.org/mailman/listinfo/python-list
Re: Datetime module
[EMAIL PROTECTED] wrote: I am writing a script that acts as an AIM bot [using twisted.IM's base scripts] and I want to add a logging feature. I got it to log who sends what to whom, but what I want to add is the date and time that the message was sent (or recieved by the bot), I tried to look at datetime on my own, and I couldn't get anything to work. Anyone know a simple way to get the current date and/or time? >>> import datetime >>> datetime.datetime.now() datetime.datetime(2005, 1, 10, 6, 39, 56, 64000) >>> print datetime.datetime.now() 2005-01-10 06:40:03.705000 >>> You may also want to consider using the logging module. // m -- http://mail.python.org/mailman/listinfo/python-list
Re: Time script help sought!
kpp9c wrote: The input would like so: [...] Attached is a first cut at a parser that actually uses the raw content of your original email. You'll notice that the net effect is that the parser instance's items attribute contains the source ordered list of items with attributes for each of the various parts of the line. From this, it should be pretty easy to adjust the times and what not. Cheers, // m #!/usr/bin/env python """usage: %prog """ raw = """I am kind of in a bit of a jam (okay a big jam) and i was hoping that someone here could give me a quick hand. I had a few pages of time calculations to do. So, i just started in on them typing them in my time calculator and writing them in by hand. Now i realize, that i really need a script to do this because: 1. It turns out there are hundreds of pages of this stuff. 2. I have to do something similar in again soon. 3. By doing it by hand i am introducing wonderful new errors! 4. It all has to be typed up anyway (which means weeks of work and even more typos!) The input would like so: Item_1TAPE_1100:238:23 Item_2TAPE_128:239:41 Item_3TAPE_139:4110:41 Item_3TAPE_1410:4711:19 Item_3TAPE_1511:2111:55 Item_3TAPE_1611:5812:10 Item_3TAPE_1712:1512:45Defect in analog tape sound. Item_3TAPE_1812:5824:20Defect in analog tape sound. Item_4TAPE_1924:33 Item_4TAPE_11025:48 Item_4TAPE_11129:48 Item_4TAPE_11231:46 Item_4TAPE_11334:17Electronic sounds. Item_4TAPE_11435:21 Item_4TAPE_11536:06 Item_4TAPE_11637:0137:38 These are analog tapes that were digitized (on to CD or a digital tape) that have now been exported as individual files that are meant to be part of an on-line audio archive. The timings refer to the time display on the CD or digital tape. The now all have to adjusted so that each item starts at 0.00 since they have all been edited out of their context and are now all individual items that start at 00:00. So Item_1 which was started at 00:23 on the tape and ended at 8:23 needs to have 23 seconds subtracted to it so that it says: Item_1TAPE_1100:0008:00 Item_2TAPE_1208:2309:41 would change to: Item_2TAPE_1200:0001:18 etc. but as always you may notice a wrinkle some items have many times (here 6) indicated: Item_3TAPE_139:4110:41 Item_3TAPE_1410:4711:19 Item_3TAPE_1511:2111:55 Item_3TAPE_1611:5812:10 Item_3TAPE_1712:1512:45Defect in analog tape sound. Item_3TAPE_1812:5824:20Defect in analog tape sound. This is all a single sound file and these separate times mark where there was a break, defect, or edit in the individual item. These have to be adjusted as well to show where these events would appear in the new sound file which now starts at 00:00. Item_3TAPE_1300:0001:00 Item_3TAPE_1401:0001:38 Item_3TAPE_1501:3802:14 Item_3TAPE_1602:1402:29 Item_3TAPE_1702:2903:04Defect in analog tape sound. Item_3TAPE_1803:0414:39Defect in analog tape sound. Further wrinkles: Some have start and end times indicated, some only start times. I suppose that the output would ideally have both some have comments and others don't ... and I need these comments echo-ed or since i probably need to make a database or table eventually non comments just have some place holder. I'd have a lot of similar type calculations to do... I was hoping and praying that some one here was feeling generous and show me the way and then, of course i could modify that to do other tasks... Usually i am happy to take the long road and all but i'll be honest, i am in a big jam here and this huge task was just dumped on me. I am frankly a little desperate for help on this and hoping someone is feeling up to spoon feeding me a clear modifiable example that works. Sorry. cheers, kevin -- http://mail.python.org/mailman/listinfo/python-list """ import optparse import re pat = re.compile('\s+') class Item: def __init__(self, line): parts = pat.split(line) self.name, self.tape, self.number, self.start = parts[:4] if len(parts) == 5: self.end = parts[4] else: self.end = None if len(parts) > 5: self.comment = ' '.join(parts[5:]) else: self.comment = None class Parser: def __init__(self): self.items = [] def feed(self, line): item = Item(line) self.items.append(item) def parseCommandLine(usage, requiredArgCount, argv=None): """Parse the command line and return (options, args). Raise an error if there are insu
Re: Best way to trap errors in ftplib?
Peter A.Schott wrote: Using ftplib.FTP object for a project we have here to upload/download files. I know that I can wrap everything in try/except blocks, but I'm having trouble getting the exact error messages out of the Exceptions. Consider using the traceback a la: try: [... whatever ...] except: import sys, traceback t, v, tb = sys.exc_info() # or use StringIO to "print" the traceback to and then log *that* traceback.print_tb(tb) // m -- http://mail.python.org/mailman/listinfo/python-list
Re: counting items
It's me wrote: Okay, I give up. What's the best way to count number of items in a list [that may contain lists]? a = [[1,2,4],4,5,[2,3]] def iterall(seq): for item in seq: try: for subitem in iterall(item): yield subitem except TypeError: yield item all = [x for x in iterall(a)] print len(all) -- http://mail.python.org/mailman/listinfo/python-list
Re: deleting from tarfile
Uwe Mayer wrote: Hi, is it possible to delete a file from a tar-archive using the tarfile module? Thanks Uwe It doesn't appear so. A workaround, of course, is to create a new file with the subset of files from the old file: #!/usr/bin/env python import tarfile import os def removeFile(filename, nameToDelete): """Remove nameToDelete from tarfile filename.""" prefix, ext = os.path.splitext(filename) newFilename = '%(prefix)s-modified%(ext)s' % locals() original = tarfile.open(filename) modified = tarfile.open(newFilename, 'w') for info in original.getmembers(): if info.name == nameToDelete: continue extracted = original.extractfile(info) if not extracted: continue modified.addfile(info, extracted) original.close() modified.close() // m -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing class variables of private classes
Uwe Mayer wrote: Hi, I need to access class variables of a class I'd like to make private: Use single underscores instead of double underscores--you won't have to workaround the name mangling. Besides, nothing's really private anyway. // m -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie inheritance question.
bwobbones wrote: Hi all, I'm a java programmer struggling to come to terms with python - bear with me! Welcome! I'm trying to subclass a class, and I want to be able to see it's attributes also. Here are my classes: [snip] class two(one): def __init__(self): print "two" The problem is that you're not calling the parent class' __init__:: class two(one): def __init__(self): one.__init__(self) ... You can also use super() to do that, but I do that so rarely I forget the syntax. Of course, that's no big deal, since:: python >>> help(super) is always to the rescue, but I'm lazy. // m -- http://mail.python.org/mailman/listinfo/python-list
Re: Assigning to self
Frans Englich wrote: Hello, I am having trouble with throwing class instances around. Perhaps I'm approaching my goals with the wrong solution, but here's nevertheless a stripped down example which demonstrates my scenario: [snip] The basic problem seems to be that you're trying to avoid creating a new instance in __init__--which is too late. By that point, the new object is already created. Rebinding the name self in __init__ doesn't do what you seem to think it will. Basically, you need to move the "only create this object if it doesn't already exist" logic outside of __init__. Here's an alternative approach: #!/usr/bin/env python class Item: def __init__(self, name): self.name = name class Factory: items = {} def getItemByName(self, name): item = Factory.items.get(name) if not item: item = Item(name) Factory.items[name] = item return item def main(): factory = Factory() name = 'foo' for x in range(10): i = factory.getItemByName(name) print i print len(factory.items) if __name__ == "__main__": main() -- http://mail.python.org/mailman/listinfo/python-list
Re: FTPLIB - retry files?
Peter A.Schott wrote: Is there any way to retry sending files with some delay up to a set number on failure? Sometimes we encounter a locked file on our server or the destination server and we want to retry that file in X seconds. In general, what's wrong with this: import time retryCount = 10 retrySleep = 5 for x in range(retryCount): try: [ftp commands] except [ftp exceptions]: time.sleep(retrySleep) else: break // m -- http://mail.python.org/mailman/listinfo/python-list
Re: Zen of Python
Timothy Fitz wrote: While I agree that the Zen of Python is an amazingly concise list of truisms, I do not see any meaning in: Flat is better than nested. I strive for balance between flat and nested. Does anyone have a good example of where this is applied? (specifically to python, or in general) One example would be using a function instead of a class when a function does the job at the module level. Using the appropriate degree of abstraction/indirection. // m -- http://mail.python.org/mailman/listinfo/python-list
Re: list item's position
Bob Smith wrote: Hi, I have a Python list. I can't figure out how to find an element's numeric value (0,1,2,3...) in the list. Here's an example of what I'm doing: Use enumerate() (new in Python 2.3, IIRC). Otherwise: for i in range(len(sequence)): item = sequence[i] ... for bar in bars: if 'str_1' in bar and 'str_2' in bar: print bar This finds the right bar, but not its list position. The reason I need to find its value is so I can remove every element in the list before it so that the bar I found somewhere in the list becomes element 0... does that make sense? Sure. You want to slice the list starting at the index of the first occurrence: index = min([i for i, item in enumerate(sequence) if 'str_1' in item and 'str_2' in item]) print sequence[index:] // m -- http://mail.python.org/mailman/listinfo/python-list
Re: Print a string in binary format
neutrino wrote: Greetings to the Python gurus, I have a binary file and wish to see the "raw" content of it. So I open it in binary mode, and read one byte at a time to a variable, which will be of the string type. Now the problem is how to print the binary format of that charater to the standard output. It seems a common task but I just cannot find the appropriate method from the documentation. Thanks a lot. How is this *not* what you want: import sys f = open(filename, 'rb') data = f.read(1) while data: sys.stdout.write(data) data = f.read(1) Of course, that's the long version of: print open(filename, 'rb').read() // m -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding a script's home directory?
Gabriel Cooper wrote: In one of my python programs has a data file I need to load. My solution was to say: if os.path.exists(os.path.join(os.getcwd(), "config.xml")): self.cfgfile = os.path.join(os.getcwd(), "config.xml") Which works fine... as long as you're *in* the script's home directory when you run it (as in, run it as: ./startApp.py as opposed to ./myApp/startApp.py). If I run it from an alternate directory the program looks for the config.xml file in my current directory not the app's home directory. So how do I get the script's home directory? import os.path dirname = os.path.dirname(__file__) // m -- http://mail.python.org/mailman/listinfo/python-list
Re: exclude binary files from os.walk
The OP wrote: > Is there an easy way to exclude binary files (I'm working on Windows XP) from the file list returned by os.walk()? Sure, piece of cake: #!/usr/bin/env python import os def textfiles(path): include = ('.txt', '.csv',) for root, dirs, files in os.walk(path): for name in files: prefix, ext = os.path.splitext(name) if ext.lower() not in include: continue filename = os.path.join(root, name) yield filename path = os.getcwd() for name in textfiles(path): print name ;-) // m -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamically pass a function arguments from a dict
Dan Eloff wrote: How can you determine that func2 will only accept bar and zoo, but not foo and call the function with bar as an argument? Let Python answer the question for you: >>> def func2(bar='a', zoo='b'): ... pass ... >>> for name in dir(func2): ... print '%s: %s' % (name, getattr(func2, name)) ... __call__: __class__: __delattr__: __dict__: {} __doc__: None __get__: __getattribute__: __hash__: __init__: __module__: __main__ __name__: func2 __new__: __reduce__: __reduce_ex__: __repr__: __setattr__: __str__: func_closure: None func_code: ", line 1> func_defaults: ('a', 'b') func_dict: {} func_doc: None func_globals: {'func2': , 'name': 'func_globals', '__builtins__': , '__name__': '__main__', 'foo : , '__doc__': None} func_name: func2 >>> for name in dir(func2.func_code): ... print '%s: %s' % (name, getattr(func2.func_code, name)) ... __class__: __cmp__: __delattr__: __doc__: code(argcount, nlocals, stacksize, flags, codestring, constants, names, varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]]) Create a code object. Not for the faint of heart. __getattribute__: __hash__: __init__: __new__: __reduce__: __reduce_ex__: __repr__: __setattr__: __str__: co_argcount: 2 co_cellvars: () co_code: d S co_consts: (None,) co_filename: co_firstlineno: 1 co_flags: 67 co_freevars: () co_lnotab: รข co_name: func2 co_names: () co_nlocals: 2 co_stacksize: 1 co_varnames: ('bar', 'zoo') >>> Hmm, func2.func_code.co_varnames seems to have the answer. Cheers, // m -- http://mail.python.org/mailman/listinfo/python-list
Re: remove strings from source
qwweeeit wrote: For a python code I am writing I need to remove all strings definitions from source and substitute them with a place-holder. To make clearer: line 45 sVar="this is the string assigned to sVar" must be converted in: line 45 sVar=s1 Such substitution is recorded in a file under: s0001[line 45]="this is the string assigned to sVar" For curious guys: I am trying to implement a cross variable reference tool and the variability (in lenght) of the string definitions (expecially if multi-line) can cause display problems. I need your help in correctly identifying the strings (also embedding the r'xx..' or u'yy...' as part of the string definition). The problem is mainly on the multi-line definitions or in cached strings (embedding chr() definitions or escape sequences). Approach this in a test-driven development way. Create sample input and output files. Write a unit test something like this (below) and run it. You'll either solve the problem yourself or ask more specific questions. ;-) Cheers, // m #!/usr/bin/env python import unittest def substitute(data): # As a first pass, just return the data itself--obviously, this should fail. return data class Test(unittest.TestCase): def test(self): data = open("input.txt").read() expected = open("expected.txt").read() actual = substitute(data) self.assertEquals(expected, actual) if __name__ == '__main__': unittest.main() -- http://mail.python.org/mailman/listinfo/python-list
Re: Iteration within re.sub()?
Bryant Huang wrote: Hi, Is it possible to perform iteration within the re.sub() function call? Sure. As the docs note: If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string. For example: #!/usr/bin/env python import re class Counter: def __init__(self): self.count = -1 def increment(self, matchObject): self.count += 1 return str(self.count) text = "abbababbaabbaaa" expected = "a01a2a34aa56aaa" # Replace all b's with an integer that increments from 0. c = Counter() pat = re.compile("(b)") actual = pat.sub(c.increment, text) assert expected == actual -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamically passing variables to unittest
Tom Haddon wrote: Hi Folks, Newbie question here. I'm trying to set up some unit testing for a database abstraction class, and the first thing I want to test is the connection parameters. So, my question is, how do I dynamically pass the variables from a list, for example to the unittest module so I can maintain the list of test cases more easily: >>> def func(*args): ... for a in args: ... print a ... >>> vars = range(10) >>> func(*vars) 0 1 2 3 4 5 6 7 8 9 >>> // m -- http://mail.python.org/mailman/listinfo/python-list