python

How do I extract text between two different matches?

I have a text file that has sets of text I need to extract that looks something like as follows: ITEM A blah blah blah ITEM B bloo bloo bloo ITEM A blee blee blee ITEM B Here is the working code I have so far: finda = r'(Item\sA)' findb = r'(Item\sB)' match_a = re.finditer(finda, usefile, 2) # the "2" is a flag to say ignore case ma...

Django blog generate next/previous entries

In a blog application (which I have mostly built following a tutorial), I would like to have a next and previous post link on the single page views of the posts. The blog app's urls.py file looks like this: from django.conf.urls.defaults import * from django.views.generic import list_detail from sandy.blog.models import Post urlpatte...

Load a .so library into ctypes

I have compiled a library using cmake add_library(object3d SHARED some_file.h some_file.cpp). After compilation, I get a file: libobject3d.so I would like to call a function in this library. This function definition in some_file.h is: void ComputeGeometryImage(char * input_image, int geometry_image_size, float * output); I did check...

Fibonacci numbers not getting printed beyond F(996)

I wrote this small snippet to calculate Fibonacci numbers. It works well for numbers up to 996 and from 997 a trace back is being printed. I can't figure out what the problem is. Does it has something to do with maximum_recursion_count? def fib(n): if n==0: return 0 elif n==1: return 1 else: return fib(n-1)+n ...

[Django] models.Model class member not appearing in model_instance._meta.fields

Hi, I have a django.contrib.contenttypes.generic.genericForeignKeyField as a member of my model, however, it is not appearing when I instantiate the model and then try to get the fields out of the _meta of the object. e.g: class A(models.Model): field2 = models.IntegerField(...) field1 = generic.genericForeignKeyField() a...

python and xml integration

How do I produce XML from Python? I built a program for keystroke detection integrated with open office and other softwares and i want the output of the program to be stored in an XML file? my program detects the keys stroked in an open office, ms office software. i want the outpu to be directly stored in an XML file ...

How to run django's test database only in memory?

My django unit tests take a long time to run, so I'm looking for ways to speed that up. I'm considering installing an SSD, but I know that has its downsides too. Of course there are things I could do with my code, but I'm looking for a structural fix. Even running a single test is slow since the database needs to be rebuilt / south mi...

Waiting on event with Twisted and PB

I have a python app that uses multiple threads and I am curious about the best way to wait for something in python without burning cpu or locking the GIL. my app uses twisted and I spawn a thread to run a long operation so I do not stomp on the reactor thread. This long operation also spawns some threads using twisted's deferToThread t...

Python string formatter for paragraphs

I'm trying to format some strings for output on the command-line, report style, and am looking for the easiest method to format a string such that I can get automatic paragraph formatting. In perlform formatting is done through the "format" function format Something = Test: @<<<<<<<< @||||| @>>>>> $str, $%, '$' ....

Python book for a non-programmer?

Are there any python books that are suited to someone who doesn't know programming already ? I am looking for something which doesn't have any perquisites (except maybe basic maths :). I have looked at dive into python, and it looks like a very good book, but much of it seems to assume you already know programming concepts (functions, et...

Is this a good true "Beginner" Python learning book?

Hi, I've learned some python, but I'll admit this is my first programming language. I like programming, and everything underlying being a programmer, but every time I attempted a programming language (C++, Java, BASIC) I failed miserably without getting so far as to get past 'Hello world!'. But with Python, I've found a new love of prog...

Convert time string expressed as <number>[m|h|d|s|w] to seconds in Python

Is there a good method to convert a string representing time in the format of [m|h|d|s|w] (m= minutes, h=hours, d=days, s=seconds w=week) to number of seconds? I.e. def convert_to_seconds(timeduration): ... convert_to_seconds("1h") -> 3600 convert_to_seconds("1d") -> 86400 etc? Thanks! ...

need help sending command to server by ssh python

hi, I am trying to connect to the server via ssh and dump the "df- h" output in some text file. p=pexpect.spawn('ssh some.some.com') i=p.expect([ssh_newkey,'password:',pexpect.EOF]) if i==0: print "I say yes" p.sendline('yes') i=p.expect([ssh_newkey,'password:',pexpect.EOF]) if i==1: p.sendline("somesome") p.expect(...

Escape html in python?

i have a <img src=__string__> but string might contain ", what should I do to escape it? Example: __string__ = test".jpg <img src="test".jpg"> doesn't work. ...

Difference between two time intervals in Python

I have two times, a start and a stop time, in the format of 10:33:26 (HH:MM:SS). I need the difference between the two times. I've been looking through documentation for Python and searching online and I would imagine it would have something to do with the datetime and/or time modules. I can't get it to work properly and keep finding ...

GUI tools and APIs for small/medium hierarchical data structures

Hi, I'm trying to find a tool and library to edit, write and read data in a hierarchical structure, similar to an LDAP tree, a Windows registry or a Berkeley DB structure. The keys should represent some hierarchy, and the values should have a relatively flexible format (typing is optional, but could be useful). Here is an example: Item...

How to put a Windows UAC Shield overlay on a button using wxPython?

I have a button that will launch a process that requires UAC elevation. I want to display the Windows UAC shield overlay on the button, how do I do this in wxPython? The application is only going to run on Windows, so I don't need to worry about it not working on other systems. edit 2: Got it: BCM_SETSHIELD = 0x0000160C btn_apply = wx....

U combinator on a fibonacci : how would you translate this code to python?

Hi, I am trying to learn about combinators and I am having trouble understand the example given at (Y overriding self-application). I think I am beginning to grasp the concept but I am still far from understanding. I would like to translate the following code to Python: (define (U f) (f f)) (define (fib-nr f) (lambda...

What are good examples of Pythonic libraries?

I'm curious of what other people consider good, Pythonic code. What I mean by Pythonic are libraries that show off good and idiomatic Python code. Adhering to the guidelines of Python. I.e good naming, correct use of modules and generally following best practices and so on. What's your favorite idiomatic Python library? Please provide ...

growing python process memory over time

My python code process memory increases dynamically as it stores dynamic data in list, dictionary and tuples wherever necessary. Though all those dynamic data is cleared physically in their variables after then, the memory is not shooting down. Hence i felt like there is a memory leak and i used gc.collect() method to collect all the u...