python

How do I check if a file exists using Python?

How do I check if a file exists, using Python. without using a try: statement? ...

How to programmatically enable/disable network interfaces? (Windows XP)

I need to enable/disable completely network interfaces from a script in Windows XP. I'm looking for a python solution, but any general way (eg WMI, some command-line à la netsh, some windows call) is welcome and will be adjusted. Thanks. ...

Why isn't the 'len' function inherited by dictionaries and lists in Python

example: a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works Python being (very) object oriented, I don't understand why the 'len' function isn't inherited by the object. Plus I keep trying the wrong solution since it appears as the logical one to me ...

Why learn Perl, Python, Ruby if the company is using C++, C# or Java as the application language?

I wonder why would a C++, C#, Java developer want to learn a dynamic language? Assuming the company won't switch its main development language from C++/C#/Java to a dynamic one what use is there for a dynamic language? What helper tasks can be done by the dynamic languages faster or better after only a few days of learning than with th...

Running multiple sites from a single Python web framework

What are come good (or at least clever) ways of running multiple sites from a single, common Python web framework (ie: Pylons, TurboGears, etc)? I know you can do redirection based on the domain or path to rewrite the URI to point at a site-specific location and I've also seen some brutish "if site == 'site1' / elseif / elseif / etc" th...

Python - time.clock() vs. time.time() - accuracy?

Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy? for example: start = time.clock() ... do something elapsed = (time.clock() - start) vs. start = time.time() ... do something elapsed = (time.time() - start) ...

Search for host with MAC-address using Python

I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas? ...

How to best implement simple crash / error reporting?

What would be the best way to implement a simple crash / error reporting mechanism? Details: my app is cross-platform (mac/windows/linux) and written in Python, so I just need something that will send me a small amount of text, e.g. just a timestamp and a traceback (which I already generate and show in my error dialog). It would be f...

What are the pros and cons of the various Python implementations?

I am relatively new to Python, and I have always used the standard cpython (v2.5) implementation. I've been wondering about the other implementations though, particularly Jython and IronPython. What makes them better? What makes them worse? What other implementations are there? I guess what I'm looking for is a summary and list of...

Switching to Python(and enjoying your work more than before)

How many of you moved from one language to Python and found it more enjoyable and generally more "fun" to code than before? Or maybe you tried Python and thought: "It is not for me, I stay with my C++/Java, Python is too simple/overrated/etc."? I moved from PHP to it(and to Django framework) and I code my projects faster, appreciating ...

Get list of XML attribute values in Python

I need to get a list of attribute values from child elements in Python. It's easiest to explain with an example. Given some XML like this: <elements> <parent name="CategoryA"> <child value="a1"/> <child value="a2"/> <child value="a3"/> </parent> <parent name="CategoryB"> <child value="b1"/> ...

If it is decided that our system needs an overhaul, what is the best way to go about it?

We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that is all over the place...

Setting Environment Variables for Mercurial Hook

I am trying to call a shell script that sets a bunch of environment variables on our server from a mercurial hook. The shell script gets called fine when a new changegroup comes in, but the environment variables aren't carrying over past the call to the shell script. My hgrc file on the respository looks like this: [hooks] changegroup...

How do you configure Django for simple development and deployment?

I tend to use SQLite when doing Django development, but on a live server something more robust is often needed (MySQL/PostgreSQL, for example). Invariably, there are other changes to make to the Django settings as well: different logging locations / intensities, media paths, etc. How do you manage all these changes to make deployment a ...

How do I unit test an __init__() method of a python class with assertRaises()?

I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise Error("foo is not equal to 1!") and a unit test that is supposed to make sure the incorrect arg passed to the constructor properly raises an error: def testInsufficientArgs(self): foo = 0 self.assertRaises((Error), myClass = MyClass(Error, ...

In Python, how do you take tokenized input such as with the C++?

In C++, I can have take input like this: cin >> a >> b >> c; And a can be int, b can be float, and c can be whatever... How do I do the same in python? input() and raw_input(), the way I'm using them, don't seem to be giving me the desired results. ...

How do I split a string into a list Python?

Learning to program, trying to do this I have a string like this 2+24*48/32 and I want to split it into a list like this ['2', '+', '24', '*', '48', '/', '32'] I have messed around with .split() but that's messy as it returns a list, which means I would now have to iterate over two strings, etc ...

What do you think about the loss of MIT's 6.001 (SICP) course?

MIT abandoned its legendary 6.001 (Structure and Interpretation of Computer Programs) course and replaced it with 6.00, 6.01 and 6.02 in the new curriculum. They are AFAIK about Python and robots. What is your opinion about the loss of SICP and Scheme in computer science education? Is it a necessary step in the right direction or a bad m...

Why should Python PEP-8 specify a maximum line length of 79 characters?

Why in this millenium should Python PEP-8 specify a maximum line length of 79 characters? Pretty much every code editor under the sun can handle longer lines. What to do with wrapping should be the choice of the content consumer, not the responsibility of the content creator. Are there any (legitimately) good reasons for adhering to 7...

In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique *while preserving order*?

For example: >>> x = [1, 1, 2, 'a', 'a', 3] >>> unique(x) [1, 2, 'a', 3] Assume list elements are hashable. Clarification: The result should keep the first duplicate in the list. For example, [1, 2, 3, 2, 3, 1] becomes [1, 2, 3]. ...