python

Is a graph library (eg NetworkX) the right solution for my Python problem?

I'm rewriting a data-driven legacy application in Python. One of the primary tables is referred to as a "graph table", and does appear to be a directed graph, so I was exploring the NetworkX package to see whether it would make sense to use it for the graph table manipulations, and really implement it as a graph rather than a complicated...

Python regex question: stripping multi-line comments but maintaining a line break

Hello, I'm parsing a source code file, and I want to remove all line comments (i.e. starting with "//") and multi-line comments (i.e. /..../). However, if the multi-line comment has at least one line-break in it (\n), I want the output to have exactly one line break instead. For example, the code: qwe /* 123 456 789 */ asd should ...

Performing a getattr() style lookup in a django template

Python's getattr() method is useful when you don't know the name of a certain attribute in advance. This functionality would also come in handy in templates, but I've never figured out a way to do it. Is there a built-in tag or non-built-in tag that can perform dynamic attribute lookups? ...

Defining dynamic functions to a string

I have a small python script which i use everyday......it basically reads a file and for each line i basically apply different string functions like strip(), replace() etc....im constanstly editing the file and commenting to change the functions. Depending on the file I'm dealing with, I use different functions. For example I got a file ...

"x Days ago' template filter in Django?

I'm looking for a filter that turns a datetime instance into 'x Days' or 'x years y months' format (as on SO). Suggestions? Am I overlooking something very obvious? ...

How to get line count cheaply in Python?

I need to get a line count of a large file (hundreds of thousands of lines) in python. What is the most efficient way both memory- and time-wise? At the moment I do: def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1 is it possible to do any better? ...

VIM: Use python 2.5 with vim 7.2

How can use Python2.5 with to write scripts in vim? I'm using vim 7.2 and have Python 2.5. Vim 7.2 seem to be linked with Python 2.4 Do I have to compile from source? ...

Problem when using MemoryDC

Why does my code print the lines gray instead of black? import wx class MyFrame(wx.Frame): def __init__(self,*args,**kwargs): wx.Frame.__init__(self,*args,**kwargs) self.panel=wx.Panel(self,-1,size=(1000,1000)) self.Bind(wx.EVT_PAINT, self.on_paint) self.Bind(wx.EVT_SIZE, self.on_size) self....

Emulating pass-by-value behaviour in python

I would like to emulate the pass-by-value behaviour in python. In other words, I would like to make absolutely sure that the function I write do not modify user supplied data. One possible way is to use deep copy: from copy import deepcopy def f(data): data = deepcopy(data) #do stuff is there more efficient or more pythonic ...

Concise vector adding in Python?

Hi, I often do vector addition of Python lists. Example: I have two lists like these: a = [0.0, 1.0, 2.0] b = [3.0, 4.0, 5.0] I now want to add b to a to get the result a = [3.0, 5.0, 7.0]. Usually I end up doing like this: a[0] += b[0] a[1] += b[1] a[2] += b[2] Is there some efficient, standard way to do this with less typing? ...

Converting a java System.currentTimeMillis() to date in python

Hello, I have timestamp in milliseconds from 1970. I would like to convert it to a human readable date in python. I don't might losing some precision if it comes to that. How should I do that ? The following give ValueError: timestamp out of range for platform time_t on Linux 32bit #!/usr/bin/env python from datetime import date prin...

How to print the comparison of two multiline strings in unified diff format?

Do you know any library that will help doing that? I would write a function that prints the differences between two multiline strings in the unified diff format. Something like that: def print_differences(string1, string2): """ Prints the comparison of string1 to string2 as unified diff format. """ ??? An usage exampl...

Canonical/Idiomatic "do what I mean" when passed a string that could a filename, URL, or the actual data to work on

It's not uncommon to see Python libraries that expose a universal "opener" function, that accept as their primary argument a string that could either represent a local filename (which it will open and operate on), a URL(which it will download and operate on), or data(which it will operate on). Here's an example from Feedparser. My ques...

using Python objects in C#

Is there an easy way to call Python objects from C#, that is without any COM mess? ...

Background process in GAE

I am developing a website using Google App engine and Django 1.0 (app-engine-patch) a major part of my program has to run in the background and change local data and also post to a remote url... can sumone suggest an effective way of doin this... ...

Unable to make an iterable decimal function in Python

I want to calculate the sum of 1/1 + 1/2 + 1/3 + ... + 1/30 I run the code unsuccessfully import decimal import math var=decimal.Decimal(1/i) for i in range(1,31): print(sum(var)) I get the error 'Decimal' object is not iterable How can you make the iterable function in Python? ...

Unit tests for automatically generated code: automatic or manual?

I know similar questions have been asked before but they don't really have the information I'm looking for - I'm not asking about the mechanics of how to generate unit tests, but whether it's a good idea. I've written a module in Python which contains objects representing physical constants and units of measurement. A lot of the units ...

Rename invalid filename in XP via Python

My problem is similar to http://stackoverflow.com/questions/497233/pythons-os-path-choking-on-hebrew-filenames however, I don't know the original encoding of the filename I need to rename (unlike the other post he knew it was Hebrew originally). I was doing data recovery for a client and copied over the files to my XP SP3 machine, and ...

OOP: good class design

My question is related to this one: Python tool that builds a dependency diagram for methods of a class. After not finding any tools I wrote a quick hack myself: I've used the compiler module, I've parsed the source code into an Abstract Source Tree and I've walked it to collect dependencies between class methods. My script generated an...

How do I represent a void pointer in a PyObjC selector?

I'm wanting to use an NSOpenPanel for an application I'm designing. Here's what I have so far: @objc.IBAction def ShowOpenPanel_(self, sender): self.panel = NSOpenPanel.openPanel() self.panel.setCanChooseFiles_(False) self.panel.setCanChooseDirectories_(True) NSLog(u'Starting OpenPanel') self.panel.beginForDirectory...