python

Do you use the get/set pattern?

Using get/set seems to be a common practice in Java (for various reasons), but I hardly see Python code that uses this. Why do you use or avoid get/set methods in Python? ...

Using set with values from a table

I'm writing a database of all DVDs I have at home. One of the fields, actors, I would like it to be a set of values from an other table, which is storing actors. So for every film I want to store a list of actors, all of which selected from a list of actors, taken from a different table. Is it possible? How do I do this? It would be a s...

How do "and" and "or" work when combined in one statement?

For some reason this function confused me: def protocol(port): return port == "443" and "https://" or "http://" Can somebody explain the order of what's happening behind the scenes to make this work the way it does. I understood it as this until I tried it: Either A) def protocol(port): if port == "443": if bool("ht...

Not Possible to Reference self in a Method Declaration?

I wanted to write a method with an argument that defaults to a member variable like so: def method(self, arg1=0, arg2=self.member): Apparently this is not allowed. Should I write it a different way, or perhaps use a value of arg2 to signal when to use the member variable? ...

Any Naive Bayesian Classifier in python?

Dear Everyone I have tried the Orange Framework for Naive Bayesian classification. The methods are extremely unintuitive, and the documentation is extremely unorganized. Does anyone here have another framework to recommend? I use mostly NaiveBayesian for now. I was thinking of using nltk's NaiveClassification but then they don't think t...

Does Python support short-circuiting?

Surprisingly, I was unable to find the answer to this on Stack Overflow: Does Python support short-circuiting in boolean expressions? ...

Optimizing Python code with many attribute and dictionary lookups

I have written a program in Python which spends a large amount of time looking up attributes of objects and values from dictionary keys. I would like to know if there's any way I can optimize these lookup times, potentially with a C extension, to reduce the time of execution, or if I need to simply re-implement the program in a compiled ...

Python initialization and circular reference counts

Can we initialize python objects with statement like this: a = b = c = None it seems to me when I did a = b = c = list() will cause circular reference count issue. Please give your expert advice. ...

How to detect if 2 news articles have the same topic? (Python language-comparison)

I'm looking for ideas on recommended approach. I'm trying to scrape some headlines and body text from articles for a few specific sites, similar to what Google does with Google News. The problem is across different sites, they may have articles on the same exact subject, worded slightly differently. Can anyone point to me what I need ...

Syntax highlighting: rich text box control for .NET

I'm looking for a free control/component/library something like a rich text box for editing codes of python (or other languages.) I like to have some features: Highlight codes Auto Indent Line numbering Defining new styles or rules of highlighting (for OpenType keywords) Is there such a control? or I have to write my own? ...

How to respond to HTTP OPTIONS request on a JSON-RPC server

My JSON-RPC client (browser using dojo JSON-RPC) makes a JSON-RPC request (dojo.callRemote) to my JSON-RPC server on myserver.com/12345 (Python 2.5, SimpleJSONRPCServer). The server then gets a HTTP request with header "OPTIONS / HTTP/1.1", which it can't handle by default, so I wrote a custom handler for this request. The request hea...

Database on the fly with scripting languages

I have a set of .csv files that I want to process. It would be far easier to process it with SQL queries. I wonder if there is some way to load a .csv file and use SQL language to look into it with a scripting language like python or ruby. Loading it with something similar to ActiveRecord would be awesome. The problem is that I don't w...

Using BeautifulSoup's findAll to search html element's innerText to get same result as searching attributes?

For instance if I am searching by an element's attribute like id: soup.findAll('span',{'id':re.compile("^score_")}) I get back a list of the whole span element that matches (which I like). But if I try to search by the innerText of the html element like this: soup.findAll('a',text = re.compile("discuss|comment")) I get back only ...

Building a financial app with Django

Hi guys, I'm building an app for a small business so I've to work with currencies, decimal numbers, etc... My goal is to create something like pulseapp.com. I've searched for opensource projects to look and the only thing I had found was django-cashflow. This app uses python-money. I've read some of the code and the ways it's coded see...

Unittest in Django. What is relationship between TestCase class and method

Hi: I am doing some unit testing stuff in Django. What is the relationship between TestCase class and the actual method in this class? What is the best practice for organizing these stuff? For example, I have class Test(TestCase): def __init__(self): ... def testTestA(self): #test code def testTestB(self)...

Unittest in Django. Static variable feeded into the test case

I want to generate some dynamic data and feed these data in to test cases. But I found that Django will initial the test class every time to do the test. So the data will get generated every time django test framework calls the function. Is there anyway to use something like the singleton or static variable to solve the problem? What s...

Passing string with (accidental) escape character loses character even though it's a raw string

I have a function with a python doctest that fails because one of the test input strings has a backslash that's treated like an escape character even though I've encoded the string as a raw string. My doctest looks like this: >>> infile = [ "Todo: fix me", "/** todo: fix", "* me", "*/", r"""//\todo stuff to fix""", "TODO f...

Live Updating Widget for 100+ concurrent users

Hi there what would you use if you had to have a div box on your website that would have to be updated constantly with new HTML content from the server. simple polling is probably not very resource inefficient - imagine also having 10'000 users and the div has to update. what is the most efficient or elegant solution for such a proble...

Pythonic mapping of an array (Beginner)

Hey StackOverflow, I've got a question related to a beginner Python snippet I've written to introduce myself to the language. It's an admittedly trivial early effort, but I'm still wondering how I could have written it more elegantly. The program outputs NATO phoenetic readable versions of an argument, such "H2O" -> "Hotel 2 Oscar", or ...

Parsing text file in python

Hello, I have html-file. I have to replace all text between this: [%anytext%]. As I understand, it's very easy to do with BeautifulSoup for parsing hmtl. But what is regular expression and how to remove&write back text data? Okay, here is the sample file: <html> [t1] [t2] ... [tood] ... [sadsada] Sample text [i8] [d9] </html> ...