python

Python: Shorten ugly code?

I have a ridiculous code segment in one of my programs right now: str(len(str(len(var_text)**255))) Is there an easy way to shorten that? 'Cause, frankly, that's ridiculous. A option to convert a number >500 digits to scientific notation would also be helpful (that's what I'm trying to do) Full code: print("Useless code rating:" , ...

Is there a structure in Python similar to C++ STL map?

Is there a structure in Python which supports similar operations to C++ STL map and complexity of operations correspond to C++ STL map? ...

SVN/python library

I need to manipulate a subversion client from python. I need to: check the most recent revision to change something under a given path. update a client to a given (head or non head) revision get logs for a given path (revisions that changed it and when). A quick search didn't turn up what I'm looking for and I'd rather not have to wr...

Why there are no ++ and -- operators in Python?

Why there are no ++/-- operators in Python? ...

How could you swap out a particular database implementation in python?

If I have a seperate class for my db calls, and I create another implementation of the db layer but say with a different data store. Is there a way for me to completly swap out the implementation without having to change allot of code? i.e. I am starting a project, so I can design things properly to achieve this from the get-go. Note:...

Django, save data from form without having to manually set each field to save ?

I can't seem to figure out a good way to do this, I have a bunch of form input fields and rather than having to do like below where I have to type out each fieldname = "" and such, is there a way to code this so it would automatically save each field from within the form? b = Modelname(fieldname=request.POST['fieldname']) b.save() I ...

Python: Listen on two ports

import socket backlog = 1 #Number of queues sk_1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk_2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) local = {"port":1433} internet = {"port":9999} sk_1.bind (('', internet["port"])) sk_1.listen(backlog) sk_2.bind (('', local["port"])) sk_2.listen(backlog) Basically, I have t...

Python routes - I'm trying to set the format extension but it's failing

I'm trying to setup my Routes and enable an optional 'format' extension to specify whether the page should load as a standard HTML page or within a lightbox. Following this http://routes.groovie.org/setting_up.html#format-extensions, I've come up with: map.connect('/info/test{.format:lightbox}', controller='front', action='test') clas...

How to access files inside a python egg file?

Folks This might be a weird requirement but it's what I've run into. I googled but yield nothing. I'm coding an application who's using a lot of constant attributes / values recorded in a XML file (they'll not change so a static file), things work fine until I generated an egg file for it. When the logic reaches the xml accessing par...

Problem getting System.ContentStatus using DSOFile.OleDocumentPropertiesClass

I've been able to read many of the Windows System Properties through Pywin32 and DSOFile, but I'm having trouble accessing the System.ContentStatus property. import win32com.client DSO = win32com.client.Dispatch("DSOFile.OleDocumentProperties") DSO.Open('C:\test.doc') print DSO.SummaryProperties.Company #works print DSO.SummaryProperti...

selection based on percentage weighting

I have a set of values, and an associated percentage for each: a: 70% chance b: 20% chance c: 10% chance I want to select a value (a, b, c) based on the percentage chance given. how do I approach this? my attempt so far looks like this: r = random.random() if r <= .7: return a elif r <= .9: return b else: return c I'...

Underline Text in Tkinter Label widget?

I am working on a project that requires me to underline some text in a Tkinter Label widget. I know that the underline method can be used, but I can only seem to get it to underline 1 character of the widget, based on the argument. i.e. p = Label(root, text=" Test Label", bg='blue', fg='white', underline=0) change underline to 0, and i...

object factory that generates an object or list of objects

I have the following code: def f(cls, value): # cls is a class # value is a string value if cls == str: pass # value is already the right type elif cls == int: value = int(value) elif cls == C1: value = C1(value) elif cls == C2: value = C2(value) elif cls == C3 # in this case, we convert the string into...

Python module to change MP3 files to play back faster?

I have a number of MP3 files containing lectures where the speaker talks very slowly and I would like to alter the MP3 file so that the playback rate is about 1.5 times as fast as normal. Can someone suggest a good Python library for this? By the way, I'm running Python 2.6 on Windows. Thanks in advance. ...

Django / Python, using iteritems() to update database gives strange error: "dictionary update sequence element #0 has length 4; 2 is required"

I'm trying to do a Django database save from a form where I don't have to manually specify the fieldnames (as I do in the 2nd code block), the way I am trying to do this is as below (1st code block) as I got the tip from another S.O. post. However, when I try this I get the error "dictionary update sequence element #0 has length 4; 2 is ...

How to test whether a variable holds a lambda?

Is there a way to test whether a variable holds a lambda? The context is I'd like to check a type in a unit test: self.assertEquals(lambda, type(myVar)) The type seems to be "function" but I didn't see any obvious builtin type to match it. Obviously I can write this, but it feels clumsy: self.assertEquals(type(lambda m: m), type(myVa...

Binary Search Trees

This is some code found on wikipedia regarding BST : # 'node' refers to the parent-node in this case def search_binary_tree(node, key): if node is None: return None # key not found if key < node.key: return search_binary_tree(node.leftChild, key) elif key > node.key: return search_binary_t...

in mechanize, is there anyway of rewriting the url to POST to in a form?

I'm running a python script and I'm using mechanize. The form i'm trying to submit normally uses javascript to rewrite the url to POST to, so to correctly submit the form i need to manually do the same. is there anyway of doing this? ...

manage.py syncdb error while Django model using non-ascii verbose_name

Hi All, I am pretty new to Django. I want the name of my models to be displayed in Chinese, so i used verbose_name in my meta class of my model, codes below: #this models.py file is encoded in unicode class TS_zone(models.Model): index = models.IntegerField() zone_name = models.CharField(max_length=50); zone_icon = models...

Google App Engine Example Modified

I took an official example of Google App Engine, that creates a Shoppinglist, and modified it so it would: Create two tables (contact and Phonenumber) instead of one Shoppinglist. This is to understand how google deals with two tables and the Foreignkey (see code below). It displays everything until line 47: data = PhoneNumber(data=se...