python

Directory listing in Python

How do I get a list of all files (and directories) in a given directory in Python? ...

Python subprocess issue with ampersands

I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting. The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've attempted quoting the command,...

Python idiom to chain (flatten) an infinite iterable of finite iterables?

Suppose we have an iterator (an infinite one) that returns lists (or finite iterators), for example one returned by infinite = itertools.cycle([[1,2,3]]) What is a good Python idiom to get an iterator (obviously infinite) that will return each of the elements from the first iterator, then each from the second one, etc. In the example...

Why does Python pep-8 strongly recommend spaces over tabs for indentation?

I see on Stack Overflow and PEP 8 that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain. Is there an underlying reason for spaces to be preferred? I would have thought that tabs were far easier to work with. ...

How can I normalize a URL in python

I'd like to know do I normalize a URL in python. For example, If I have a url string like : "http://www.example.com/foo goo/bar.html" I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL. ...

How do I get the modified date/time of a file in Python?

How do I get the modified date/time of a file in Python? ...

Accessing object memory address

When you call the object.__repr__() method in python you get something like this back: <__main__.Test object at 0x2aba1c0cf890>, is there any way to get a hold of the memory address if you overload __repr__(), other then calling super(Class, obj).__repr__() and regexing it out? ...

How do I include a stacktrace in my Django 500.html page?

I'm running Django 1.0 and I'm close to deploying my app. As such, I'll be changing the DEBUG setting to False. With that being said, I'd still like to include the stacktrace on my 500.html page when errors occur. By doing so, users can copy-and-paste the errors and easily email them to the developers. Any thoughts on how best to app...

How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python?

Yesterday, I asked this question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such as Ruby without having to be extremely imperative in style. Since I didn't see anything I really liked, I've written the solution I was lo...

How do you translate this regular-expression idiom from Perl into Python?

I switched from Perl to Python about a year ago and haven't looked back. There is only one idiom that I've ever found I can do more easily in Perl than in Python: if ($var =~ /foo(.+)/) { # do something with $1 } elsif ($var =~ /bar(.+)/) { # do something with $1 } elsif ($var =~ /baz(.+)/) { # do something with $1 } The corres...

How do I find the location of my Python site-packages directory?

How do I find the location of my site-packages directory? ...

Is there an Eclipse add-on to build a python executable for distribution?

I want to build an executable to distribute to people without python installed on their machines. Is there an add-on to Eclipse that allows this? I couldn't find one. If not, do you have a builder that you recommend that would make it easy to go to my python project directory created in Eclipse, and bundle it all up? Thanks, Mark ...

Need to create a layered dict from a flat one

I have a dict, that looks like this: { 'foo': { 'opt1': 1, 'opt2': 2, }, 'foo/bar': { 'opt3': 3, 'opt4': 4, }, 'foo/bar/baz': { 'opt5': 5, 'opt6': 6, } } And I need to get it to look like: { 'foo': { 'opt1': 1, 'opt2': 2, ...

How do I copy a file in python?

How do I copy a file in python? I couldn't find anything under os. ...

Passing apache2 digest authentication information to a wsgi script run by mod_wsgi

I've got the directive <VirtualHost *> <Location /> AuthType Digest AuthName "global" AuthDigestDomain / AuthUserFile /root/apache_users <Limit GET> Require valid-user </Limit> </Location> WSGIScriptAlias / /some/script.wsgi WSGIDaemonProcess mywsgi user=someuse...

How to get/set logical directory path in python.

In python is it possible to get or set a logical directory (as opposed to an absolute one). For example if I have: /real/path/to/dir and I have /linked/path/to/dir linked to the same directory. using os.getcwd and os.chdir will always use the absolute path >>> import os >>> os.chdir('/linked/path/to/dir') >>> print os.getcwd() /...

Passing around urls between applications in the same project

I am trying to mock-up an API and am using separate apps within Django to represent different web services. I would like App A to take in a link that corresponds to App B and parse the response (json). Is there a way to dynamically construct the url to App B so that I can test the code in development and not change to much before going...

Comparison of Python and Perl solutions to Wide Finder challenge

I'd be very grateful if you could compare the winning O’Rourke's Perl solution to Lundh's Python solution, as I don't know Perl good enough to understand what's going on there. More specifically I'd like to know what gave Perl version 3x advantage: algorithmic superiority, quality of C extensions, other factors? Wide Finder: Results ...

I know Perl 5. What are the advantages of learning Perl 6, rather than moving to Python?

Coming from a Perl 5 background, what are the advantages of moving to Perl 6 or Python? Edit: If you downvoted this because you think it's just flamebait, read the answers below. They're not raving arguments; they're well-written discussions of the pros and cons of each language. Give the Stack Overflow community some credit. ...

What is the intended use of the DEFAULT section in config files used by ConfigParser?

I've used ConfigParser for quite a while for simple configs. One thing that's bugged me for a long time is the DEFAULT section. I'm not really sure what's an appropriate use. I've read the documentation, but I would really like to see some clever examples of its use and how it affects other sections in the file (something that really ill...