python

Handling graceful degradation within a Django form

I have a form that looks similar to the following (simplified for brevity): PRICING_PATTERN = r'(?:^\$?(?P<flat_price>\d+|\d?\.\d\d)$)|(?:^(?P<percent_off>\d+)\s*\%\s*off$)' class ItemForm(forms.Form): pricing = forms.RegexField( label='Pricing', regex=PRICING_PATTERN ) pricing_type = forms.CharField( ...

Is there a B-Tree Database or framework in Python?

I heard that B-Tree datbases are faster than Hash tables, so I thought of using a B-Tree Db for my project. Is there any existing framework in python which allows us to use such Data structure or will I have to code from scratch? ...

Reportlab - Pie chart fill colors

I am making a simple pie chart in reportlab. I can not find in the docs how to change each individual pies fill color. pie_chart = Drawing(200, 200) pc = Pie() pc.x = 65 pc.y = 15 pc.width = 150 pc.height = 150 pc.data = [65,13,12,9,1] pc.labels = ['Name','Another','Yet Another','Test','Stack'] pc.slices[1]. // This is where I need t...

MEDIA_URL tuple

I would like to write a context_processor, something like this: settings.py: MEDIA_URLS = ('cname2.example.com/media', 'cname3.example.com/media',) TEMPLATE_CONTEXT_PROCESSORS = ( "util.context_processors.media", ) util/context_processors.py from random import choice from django.conf import settings def media(request): """ ...

Python Range Class/Subclass

I have code for a Range class like this: class Range: def __init__(self, start, end): self.setStart(start) self.setEnd(end) def getStart(self): return self.start def setStart(self, s): self.start = s def getEnd(self): return self.end def setEnd(self, e): self.end = e def getLength(se...

Plotting mplot3d / axes3D xyz surface plot with log scale?

Hi I've been looking high and low for a solution to this simple problem but I can't find it anywhere! There are a loads of posts detailing semilog / loglog plotting of data in 2D e.g. plt.setxscale('log') however I'm interested in using log scales on a 3d plot(mplot3d). I don't have the exact code to hand and so can't post it here, how...

How to stub Python methods without Mock

I'm a C# dev moving into some Python stuff, so I don't know what I'm doing just yet. I've read that you don't really need Dependency Injection with Python. I've been told you can instantiate objects in your code and have them run the way you want, however, you can point methods on those objects to my own stubs defined in my tests - supp...

Python Servers fighting each others with sockets

I try to make two servers in a file, but they are fighting each other visibly have anyone an idea to make them peace ? here is my code : # -*- coding: utf-8 -*- import socket import sys import re import base64 import binascii import time import zlib import sys import StringIO import contextlib import smtplib from threading import Threa...

Facebook python SDK: Is there a request that gets the information in the paging urls?

I'm using the fantastic python-sdk wrapper for the facebook graph api in python. If I want to get the information on a friend's wall: result = graph.get_connections( friendUID , "feed") It returns a bunch of wall posts and a pair of URLs to access next/previous posts in result["data"]["paging"] It looks like {u'next': u'https://grap...

How to generate a predictable shuffling of a sequence without generating the whole sequence in advance?

The following python code describes exactly what I want to achieve for a sequence of arbitrary size (population): import random fixed_seed = 1 #generate the same sequence every time with a fixed seed population = 1000 sample_count = 5 #demonstration number num_retries = 3 #just enough to show the repeatable behaviour for trynum in xran...

how to make a Command Line Interface or Interpreter in python

Hi, guys. I have seen some CLI questions here, but I still want to ask this question for more detailed answers. I have already developed class1.py, class2.py, etc. with functions implemented inside each class. e.g. Operator.py has add, minus,time, devide functions. how can I build a command line interface for these classes? also for ...

What is the best language for fast web development?

I have an idea for a simple web application and I've heard a lot about Ruby, Ruby on Rails, Python, PHP, etc. But what language do you think is better to have working prototypes and get the application running online really quick? NOTE: This is not a question about the language's performance, I am just trying to find the best language...

sandbox to execute possibly unfriendly python code

Hi, everyone. Let's say there is a server on the internet that one can send a piece of code to for evaluation. At some point server takes all code that has been submitted, and starts running and evaluating it. However, at some point it will definitely bump into "os.system('rm -rf *')" sent by some evil programmer. Apart from "rm -rf" yo...

ndarray field names for both row and column?

I'm a computer science teacher trying to create a little gradebook for myself using NumPy. But I think it would make my code easier to write if I could create an ndarray that uses field names for both the rows and columns. Here's what I've got so far: import numpy as np num_stud = 23 num_assign = 2 grades = np.zeros(num_stud, dtype=[(...

mplot3d - how do I display minor ticks?

Hi How do I display (turn on) minor ticks on a 3D surface plot using mplot3d / axes3d graph? I can define the major tickmark locations using: ax.w_zaxis.set_major_locator(MultipleLocator(10)) Similarly, I'm guessing I can define the the minor ticks using: ax.w_zaxis.set_minor_locator(MultipleLocator(2)) This appears to define the m...

How to make NSLog work with Python's logging module when using PyObjC?

I'm writing a Django-based webapp that imports a Cocoa framework via PyObjC. The Cocoa framework has NSLog() littered all through it and while I can see them when running the Django server in non-daemon mode, as soon as I go to daemon I simply lose all this useful NSLog() output. Is there any easy way to get NSLog stuff to bubble up in...

django heterogeneous queryset proxy models

Howdy, I am trying to figure out how to use proxy classes in Django. I want to receive a queryset where each object belongs to a proxy class of a common super class so that I can run custom sub-classed methods with the same name and my controller logic doesn't need to know or care about which kind of Proxy model it is working with. One ...

python SocketServer.BaseRequestHandler knowing the port and use the port already opened ....

This is the code which i played, but each time i make a mistake i can't relaunch it. It says to me that the port / socket is already used That's the first question The second one is in my MyTCPHandler how can i kno the port used ? here is my code : # MetaProject v 0.2 # -*- coding: utf-8 -*- """ Thanks to : People from irc : Flox,Luyt ...

python beautifulsoup related problem

hello All. i have some problem to extract some data from html source. following is sniffit of my html source code, and i want to extract string value in every following <td class="gamedate">10/12 00:59</b></td> <td class="gametype">오버언더</b></td> <td class="legue"><nobr style="width:100%;overflow:hidden;letter-spacing:-1;font-size...

Twisted HTTP Proxy Channel set to None on LostConnection but loseConnection hasn't been called yet

Ok I have been writing a proxy to take http GET requests and translate them into HTTP POST requests (because a lot of media players for python only support GET). So I know am working on caching those results that way I only download a url once, I moved a lot of code from the super class to the sub class and changed it so that I could sen...