Does __import__ require a module to have a .py suffix?

2008-03-12 Thread mrstephengross
Hi all. I've got a python file called 'foo' (no extension). I want to be able to load it as a module, like so: m = __import__('foo') However, the interpreter tells me "No module named foo". If I rename it foo.py, I can indeed import it. Is the extension required? Is there any way to override th

Re: Instance of inherited nested class in outer class not allowed?

2008-02-27 Thread mrstephengross
> class Foo: > foo = Foo() > > You have to live with that. Just do > Outer.foo = Outer.Parent() > after your class-statement to achieve the same result. Hmmm. Well, I see why that works. It's too bad, though. If I want to keep all executed code safely within a "if __name__ == '__main__'" blo

Instance of inherited nested class in outer class not allowed?

2008-02-27 Thread mrstephengross
I've got an interesting problem with my class hierarchy. I have an outer class, in which two nested classes are defined: class Outer: class Parent: def __init__ (self): print "parent!" class Child(Parent): def __init__ (self): Outer.Parent.__init__(self) foo = Child() No

Re: How to turn a list of tuples into a dictionary?

2008-02-26 Thread mrstephengross
> How about this? > d = dict(tuples) Aha! I hadn't realized it could be so simple. --Steve -- http://mail.python.org/mailman/listinfo/python-list

How to turn a list of tuples into a dictionary?

2008-02-26 Thread mrstephengross
Let's say I've got a list of tuples, like so: ( ('a', '1'), ('b', '2'), ('c', '3') And I want to turn it into a dictionary in which the first value of each tuple is a key and the second value is a value, like so: { 'a' -> '1', 'b' -> '2', 'c' -> '3' } Is there a way to do this with a single

Re: How to manipulate elements of a list in a single line of code?

2008-02-25 Thread mrstephengross
> Yes, it's called a "list comprehension", and is many people's favourite Awesome! Thanks, --Steve -- http://mail.python.org/mailman/listinfo/python-list

How to manipulate elements of a list in a single line of code?

2008-02-25 Thread mrstephengross
I would like to translate the contents of a list. For instance, let's say I've got a list of strings and I want to append "foo" to each element. I might do the following; list1 = ['a', 'b', 'c'] for i in range(0, len(list1)): list1[i] += 'foo' Ok, that much works. But what if I don't want to

Re: Return value of an assignment statement?

2008-02-21 Thread mrstephengross
> What you can't do (that I really miss) is have a tree of assign-and-test > expressions: > import re > pat = re.compile('some pattern') > if m = pat.match(some_string): > do_something(m) Yep, this is exactly what I am (was) trying to do. Oh well Any clever

Return value of an assignment statement?

2008-02-21 Thread mrstephengross
Hi all. In C, an assignment statement returns the value assigned. For instance: int x int y = (x = 3) In the above example, (x=3) returns 3, which is assigned to y. In python, as far as I can tell, assignment statements don't return anything: y = (x = 3) The above example generates a Syn

Re: Print a list to a string?

2007-11-01 Thread mrstephengross
> >>> import cStringIO > >>> s = cStringIO.StringIO() > >>> print >>s, [1, 2, 1.0/5, 'hello world'] > >>> s.getvalue() Thanks--this works perfectly! -_Steve -- http://mail.python.org/mailman/listinfo/python-list

Print a list to a string?

2007-10-31 Thread mrstephengross
I would like to get the results of a print operation placed in a string. For instance, you can very easily create a list and print it to stdout: x = [1,2,3] print x # Will print [1,2,3] What if I want the text "[1,2,3]" placed in a string? For instance, something like: x = [1,2,3] str =

Easiest way to get exit code from os.popen()?

2007-10-24 Thread mrstephengross
Hi folks. I'm using os.popen() to run a command; according to the documentation, the filehandle.close() oepration is suppsoed to return the exit code. However, when I execute something like "exit 5", close() returns 1280. Here's the code: pipe = os.popen("exit 5") print pipe.close() # prints 1

Re: How to find out which functions exist?

2007-10-24 Thread mrstephengross
> import module > from inspect import getmembers, isclass > classes = getmembers(module, isclass) Ok, this makes sense. How can I do it inside the .py file I'm working on? That is, consider this: class A: pass class B: pass import inspect print inspect.getmembers(, inspect.isclass

Re: How to find out which functions exist?

2007-10-23 Thread mrstephengross
> Take a look at the `issubclass()` function. Ok, I see how to use issubclass(). How can I get a list of classes present in the file? --Steve -- http://mail.python.org/mailman/listinfo/python-list

How to find out which functions exist?

2007-10-23 Thread mrstephengross
Let's say I have a python file with a base class, and a few derived classes: class Base: pass class Derived1(Base): pass class Derived2(Base): pass Is there a way I can find out the classes that have been derived from Base? Thanks, --Steve -- http://mail.python.org/mailman/listinfo/pyt

Can't refer to base class attribute?

2007-10-23 Thread mrstephengross
I've got a Base class with an attribute "foo" (of type Foo), and a Derived class (derived from Base). In Derived's constructor, I try to refer to Base.foo, but python complains: AttributeError: class Base has no attribute 'foo' Any ideas? (code below) === CODE === #!/usr/bin/python class Foo:

Urllib2 / add_password method

2006-04-28 Thread mrstephengross
I'm working on learning how to use urllib2 to use a proxy server. I've looked through the postings on this group, and it's been helpful. I have not, however, found complete documentation on the add_password() functions. Here's what I've got so far: # import

Embedding a python module in libpython.a

2006-03-29 Thread mrstephengross
Ok, I'm trying to build libpython.a. It's version 1.5.2 (I know it's an old version, but I need to get it to work if at all possible). I want to find a way to make sure the module copy_reg is present in the libpython.a. I know all about editing the Modules/Setup file to put in additional modules, b

McMillan installer on solaris - complains about missing .pkg file

2006-03-10 Thread mrstephengross
I'm trying to get the mcmillan installer utility to generate a standalone executable for me. I've gotten to work--almost!--but still have one problem. After running Installer's Build.py on my script/spec, it appears to work. I go into the directory generating by Build.py and run my program. It work

Newbie ZSI / syntax question

2006-03-06 Thread mrstephengross
I'm getting started working with ZSI. I have a client script that looks like this: === import ZSI.client b = ZSI.client.Binding() b.Send('http://...', 'verifyUserRegistered', '00:00:00:00:00:00') === When I run it, I get the following error: ==

How to force creation of a .pyc?

2006-02-22 Thread mrstephengross
I would like to distribute a python program, but only in .pyc form (so that people cannot simply look at my code). Is there a way to do this? I've read up a little on the logic by which python creates .pyc's, and it sounds like python requires the main executed program to be in .py format. Any idea

Is there a way to build python without 'posixmodule' ?

2006-02-21 Thread mrstephengross
I'm working on building python 2.4.2 with the mingw compiler (on cygwin). It runs into problems with Modules/posixmodule.c, because the mingw compiler does *not* provide complex process management functionality. I can exclude posixmodule.c from the python build process, but gcc still fails when it

Is there a way to build python without 'posixmodule' ?

2006-02-21 Thread mrstephengross
I'm working on building python 2.4.2 with the mingw compiler (on cygwin). It runs into problems with Modules/posixmodule.c, because the mingw compiler does *not* provide complex process management functionality. I can exclude posixmodule.c from the python build process, but gcc still fails when it

Building python 2.4.2 on Cygwin

2006-02-20 Thread mrstephengross
Ok, I'm working on building python 2.4.2 on cygwin. I *think* it's version 3.0 or 3.1 (is there a quick way to find out what version of cygwin is running within a shell?) Anyway, it appears to 'configure' fine, but gcc reports a compile error when it gets to posixmodule.c. Here's the first few lin

Another simple freeze question

2006-02-17 Thread mrstephengross
Is there a user manual for freeze.py? --Steve ([EMAIL PROTECTED]) -- http://mail.python.org/mailman/listinfo/python-list

Simple question about freeze

2006-02-17 Thread mrstephengross
I'm working with the freeze utility, and I'm trying to learn the basics. For the most part, I think I understand how it works. I have a question about modules, though. I want to make sure that my native executable is entirely standalone. That is, it should have no dynamic linking. I've read throug

Re: Embedding a binary file in a python script

2006-02-15 Thread mrstephengross
Good point I appreciate all the help, and apologize if I came across badly. I'm definitely willing to put in the work to understand all this, it's just that it's a lot of new modules for me and I'm a bit overwhelmed. Sorry if I seemed impatient... -- http://mail.python.org/mailman/listinfo/py

Re: Embedding a binary file in a python script

2006-02-15 Thread mrstephengross
Ok, this looks really cool, but can you explain a little more step-by-step what's going on? In the end, I need to have a single python script that (1) contains the archive and (2) can extract that archive. The example you've given is interesting, but it's not clear to me how to create the actual py

Re: Embedding a binary file in a python script

2006-02-15 Thread mrstephengross
Ok, this is a neat idea... The uu module deals with files though, not strings. Is there a way in python to make a string act like a file handle? Example: my_string = "uu-encoded-stuf.." my_out_file_handle = ?? # What should this variable look like? import uu uu.decode(my_string, my_out_file_h

Embedding a binary file in a python script

2006-02-15 Thread mrstephengross
I want to find a way to embed a tar file *in* my python script, and then use the tarfile module to extract it. That is, instead of distributing two files (extractor.py and archive.tar) I want to be able to distribute *one* file (extractor-with-embedded-archive.py). Is there a way to do this? Than

Which SQL module to use?

2005-10-04 Thread mrstephengross
I'd like to do some basic SQL stuff in Python. It seems like there are a heck of a lot of SQL modules for Python. What's the simplest and easiest one to use? Thanks, --Steve ([EMAIL PROTECTED]) -- http://mail.python.org/mailman/listinfo/python-list

Re: How to detect a double's significant digits

2005-05-05 Thread mrstephengross
>This doesn't look like Python to me. Are you sure you're on the right newsgroup? Er, ok, I'm an idiot. This was all supposed to be on comp.lang.c++, but obviously I posted on the wrong one. Sorry for all the hassle. In python, this stuff is a heck of a lot easier. --Steve -- http://mail.pytho

Re: How to detect a double's significant digits

2005-05-05 Thread mrstephengross
>But, assuming you have your numbers as strings, I would suggest looking at str.split() and len(). Well, the numbers are in fact stored as numbers, so string processing won't work. >I'd give you an example, but this sounds kinda like a homework assignment. The task may sound like it comes from c

Re: How to detect a double's significant digits

2005-05-05 Thread mrstephengross
Ok, that won't work. First of all, str() is not a function. If I want to convert the float into a string, the conversion function will have to use some kind of numeric precision, which will screw things up. Consider this: float f = 1.004; ostringstream s; s << f; cout << s.str(); The above code m

Re: How to detect a double's significant digits

2005-05-05 Thread mrstephengross
So how can I get the kind of information I want then? For example: 0.103 --> 3 0.0103 --> 4 0.00103 --> 5 0.000103 --> 6 0.103 --> 7 Any ideas? --Steve -- http://mail.python.org/mailman/listinfo/python-list

How to detect a double's significant digits

2005-05-05 Thread mrstephengross
Hi all... How can I find out the number of significant digits (to the right of the decimal place, that is) in a double? At least, I *think* that's what I'm asking for. For instance: 0.103 --> 3 0.0103 --> 4 0.00103 --> 5 0.000103 --> 6 0.103 --> 7 Thanks in advance! --Steve ([EMAIL PROTECTED]

Re: MinGW building with Python 2.4

2005-03-23 Thread mrstephengross
> Since the only official way to do this is pymingw, you should at least > give feedback of the steps you followed, and what didn't work (1) I have Cygwin 5.1 with GCC 3.3.1 on it. (2) I unzipped Python 2.4. (3) I unzipped pyMinGW-24-0064.zip into that directory, overwriting the appropriate files.

MinGW building with Python 2.4

2005-03-22 Thread mrstephengross
Ok, I know there are already a million posts on this group about getting Python to build with MinGW. I've been through many of them, and have still not found a good comprehensive way to accomplish this. I've got Cygwin 5.1 with GCC 3.3.3 on it. I'm using Python 2.4. Note: You invoke the mingwin c