python

Elegant way to remove items from sequence in Python?

When I am writing code in Python, I often need to remove items from a list or other sequence type based on some criteria. I haven't found a solution that is elegant and efficient, as removing items from a list you are currently iterating through is bad. For example, you can't do this: for name in names: if name[-5:] == 'Smith': ...

How to check set of files conform to a naming scheme

I have a bunch of files (TV episodes, although that is fairly arbitrary) that I want to check match a specific naming/organisation scheme.. Currently: I have three arrays of regex, one for valid filenames, one for files missing an episode name, and one for valid paths. Then, I loop though each valid-filename regex, if it matches, appen...

Build a Basic Python Iterator

How would one create an iterative function (or iterator object) in python? ...

A Transpose/Unzip Function in Python

I have a list of 2-item tuples and I'd like to convert them to 2 lists where the first contains the first item in each tuple and the second list holds the second item. For example: original = [('a', 1), ('b', 2), ('c', 3), ('d', 4)] # and I want to become... result = (['a', 'b', 'c', 'd'], [1, 2, 3, 4]) Is there a builtin function th...

Introducing Python

The company I used to work with has two developers working fulltime, and a handful of freelancers. They're in the process of hiring a new lead developer to try to bring order and management to the development. But, currently, one of the developers has seen the light of Django (the company has only developed in PHP to date) while the oth...

What's the best way to grab/parse command line arguments passed to a Python script?

The title says it all...what's the easiest, tersest, and most flexible method or library for parsing Python command line arguments? ...

How to create a temporary file (for writing to) in C#?

I'm looking for something like the tempfile module in Python: A (preferably) secure way to open a file for writing to. This should be easy to delete when I'm done too... It seems, .NET does not have the "batteries included" features of the tempfile module, which not only creates the file, but returns the file descriptor (old school, I k...

Find broken symlinks with Python.

If I call os.stat() on a broken symlink, python throws an OSError exception. This makes it useful for finding them. However, there are a few other reasons that os.stat() might throw a similar exception. Is there a more precise way of detecting broken symlinks with Python under Linux? ...

updating an auto_now DateTimeField in a parent model w/ Django

I've got two models: Message and Attachment. Each attachment is attached to a specific message, using a ForeignKey on the Attachment model. Both models have an auto_now DateTimeField called updated. I'm trying to make it so that when any attachment is saved, it also sets the updated field on the associated message to now. Here's my code:...

Why Java and Python garbage collection methods are different?

Python uses reference count method to handle object life time. So an object has no more used and it will be immediately destroyed. But, in Java, GC(garbage collector) destroys objects which are no more used at specific time. Why Java choose this strategy and what is benefit from this? Is this better than Python approach? ...

Why does this python date/time conversion seem wrong?

>>> import time >>> time.strptime("01-31-2009", "%m-%d-%Y") (2009, 1, 31, 0, 0, 0, 5, 31, -1) >>> time.mktime((2009, 1, 31, 0, 0, 0, 5, 31, -1)) 1233378000.0 >>> 60*60*24 # seconds in a day 86400 >>> 1233378000.0 / 86400 14275.208333333334 time.mktime should return the number of seconds since the epoch. Since I'm giving it a time at mi...

Zemanta/Open Calais, how do they do that.

Hi, I was wondering how as semantic service like Open Calais figures out the names of companies, or people, tech concepts ,keywords etc from a piece of text. Is it because they have a large database that they match the text against? How would a service like Zemanta know what images to suggest to a piece of text for instance? Was hop...

Where can open source developers "showcase" their projects and ask for help with development?

After not having much luck with writing to the Python mailing list, I've been on a search for the appropriate place to post my project to and receive developer support. I don't want to blacklist it by posting it to a general site and make it appear as spamming, but I also want to make sure it gets seen by as many developers with experie...

Unicode vs UTF-8 confusion in Python / Django?

I stumbled over this passage in the Django tutorial: Django models have a default str() method that calls unicode() and converts the result to a UTF-8 bytestring. This means that unicode(p) will return a Unicode string, and str(p) will return a normal string, with characters encoded as UTF-8. Now, I'm confused because afaik Unicode...

Format numbers to strings in Python

I need to find out how to format numbers as strings. My code is here: return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of my string outputting "5:30:59.07 pm", it would displa...

How do I download a file over HTTP using Python?

I have a small utility that I use to download an MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes. The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows .bat file to download the actual MP3 however. I would prefer to have ...

What's the best way to duplicate fork() in windows?

How do I implement some logic that will allow me to duplicate the functionality on windows that I have on linux with fork() using python? I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking or waiting. ...

How can I graph the Lines of Code history for git repo?

Basically I want to get the number of lines-of-code in the repository after each commit. The only (really crappy) ways I have found is to use git filter-branch to run "wc -l *", and a script that run git reset --hard on each commit, then ran wc -l To make it a bit clearer, when the tool is run, it would output the lines of code of the ...

Python code generator for Visual Studio?

I had an idea, if I add a python .py file to my C# project, and tag the file with a custom generator that would execute the python file, and treat the output as the result of the code generation, ie. put it into a C# file, that would allow me to do quite a lot of code generation as part of the build process. Does anyone know if such a c...

How to capture Python interpreter's and/or CMD.EXE's output from a Python script?

Is it possible to capture Python interpreter's output from a Python script? Is it possible to capture Windows CMD's output from a Python script? If so, which librar(y|ies) should I look into? ...