Re: remove strings from source

2005-02-26 Thread Mark McEahern
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:

Re: Dynamically pass a function arguments from a dict

2005-02-23 Thread Mark McEahern
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, na

Re: exclude binary files from os.walk

2005-01-27 Thread Mark McEahern
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

Re: Finding a script's home directory?

2005-01-24 Thread Mark McEahern
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 h

Re: Print a string in binary format

2005-01-20 Thread Mark McEahern
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 o

Re: list item's position

2005-01-19 Thread Mark McEahern
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:

Re: Zen of Python

2005-01-19 Thread Mark McEahern
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)

Re: FTPLIB - retry files?

2005-01-17 Thread Mark McEahern
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 = 1

Re: Assigning to self

2005-01-17 Thread Mark McEahern
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 creat

Re: Newbie inheritance question.

2005-01-16 Thread Mark McEahern
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 i

Re: accessing class variables of private classes

2005-01-16 Thread Mark McEahern
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-lis

Re: deleting from tarfile

2005-01-15 Thread Mark McEahern
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(filena

Re: counting items

2005-01-12 Thread Mark McEahern
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: yie

Re: Best way to trap errors in ftplib?

2005-01-11 Thread Mark McEahern
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 ...]

Re: Time script help sought!

2005-01-11 Thread Mark McEahern
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

Re: Datetime module

2005-01-10 Thread Mark McEahern
[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 da

Re: printing line numbers for debugging purpose

2005-01-08 Thread Mark McEahern
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:

Re: Building unique comma-delimited list?

2005-01-05 Thread Mark McEahern
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

Re: ? about file() and open()

2005-01-02 Thread Mark McEahern
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

Re: Rebinding stdout

2005-01-02 Thread Mark McEahern
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 thi

Re: Looping using iterators with fractional values

2005-01-01 Thread Mark McEahern
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

Re: Dynamically passing variables to unittest

2004-12-14 Thread Mark McEahern
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

Re: Iteration within re.sub()?

2004-12-14 Thread Mark McEahern
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. Fo