python

Remove duplicates in a list while keeping its order (Python)

This is actually an extension of this question. The answers of that question did not keep the "order" of the list after removing duplicates. http://stackoverflow.com/questions/1534736/how-to-remove-these-duplicates-in-a-list-python biglist = [ {'title':'U2 Band','link':'u2.com'}, {'title':'Live Concert by U2','link':'u2.com...

Why does this happen in my template for Django?

simple%20minds is displayed when do this: {{ rec.artist_name }} How do I remove the %20...and make it spaces? When I put | safe as a fitler, the error is: Could not parse the remainder: ' | safe' from 'active.artist_name | safe' Thanks. ...

How to capitalize the first letter of each word in a string (Python)?

s = 'the brown fox ' ...do something here... s should be 'The Brown Fox' What's the easiest way to do this? ...

Regex From .NET to Python

I have a regular expression which works perfectly well (although I am sure it is weak) in .NET/C#: ((^|\s))(?<tag>\@(?<tagname>(\w|\+)+))(?($|\s|\.)) I am trying to move it over to Python, but I seem to be running into a formatting issue (invalid expression exception). It is a lame question/request, but I have been staring at this fo...

Difference between defining a member in __init__ to defining it in the class body in python?

What is the difference between doing class a: def __init__(self): self.val=1 to doing class a: val=1 def __init__(self): pass ...

Unexpected result on a simple example

# Barn yard example: counting heads and legs def solve(numLegs, numHeads): for numChicks in range(0, numHeads + 1): numPigs = numHeads - numChicks totLegs = 4*numPigs + 2*numChicks if totLegs == numLegs: return [numPigs, numChicks] return [None, None] def barnYard(heads, legs): pigs, chickens = solve(l...

Differences between isinstance() and type() in python

What are the differences between these two code fragments? Which way is considered to be more pythonic? Using type(): import types if type(a) is types.DictType: do_something() if type(b) in types.StringTypes(): do_something_else() Using isinstance(): if isinstance(a, dict): do_something() if isinstance(b, str) or isinst...

Understanding an example

def solve(numLegs, numHeads): for numChicks in range(0, numHeads + 1): numPigs = numHeads - numChicks totLegs = 4*numPigs + 2*numChicks if totLegs == numLegs: return [numPigs, numChicks] return [None, None] def barnYard(heads, legs): pigs, chickens = solve(legs, heads) if pigs == None: print "Th...

Problem about python writing files

I've got a weird problem with python programming. I used the statement'writelines()' to write a series of lists into a new file.During the process I could see the context in the file icon via preview, however once after the program finished running the output file comes out to be a blank file. In short, my problem is that the program do...

How to assign a variable in IF, and then return it. (Python)

def isBig(x): if x > 4: return 'apple' else: return 'orange' This works: if isBig(y): return isBig(y) This does NOT work: if fruit = isBig(y): return fruit Why doesn't the 2nd one work!? I want a 1-liner. Except, the 1st one will call the function TWICE. How to make it 1 liner, without calling the function ...

Why doesn't memcache work in my Django?

from django.core.cache import cache def testcache(): cache.set('test','I am putting this message in',3333) print cache.get('test') It just prints "None" This is in "ps aux": dovr 2241 0.0 0.8 57824 2144 ? Ssl 04:20 0:00 memcached -d -u root -m 3900 -p 11211 dovr 2247 0.0 3.7 83696 9800 ...

python setup.py uninstall

I have installed a python package with python setup.py install. How do I uninstall it? ...

Counting all the keys pressed and what they are (python)

I'd like to create a map of the number of presses for every key for a project I'm working on. I'd like to do this with a Python module. Is it possible to do this in any way? ...

web2py - how to inject html

i used rows.xml() to generate html output. i want to know how to add html codes to this generated html page e.g: "add logo, link css file,.. etc" rows=db(db.member.membership_id==request.args[0]).select(db.member.membership_id ,db.member.first_name,db.member.middle_name ...

I do urllib2 and I download the htmlSource of the webpage. How do I make this all on 1 line? (Python)

urlReq = urllib2.Request(theurl) urlReq.add_header('User-Agent',random.choice(agents)) urlResponse = urllib2.urlopen(urlReq) htmlSource = urlResponse.read() How do I make htmlSource in 1 line, instead of many lines? ...

Python: is using "..%(var)s.." % locals() a good practice ?

I discovered this pattern (or anti-pattern) and I am very happy with it. I feel it is very agile: def example(): age = ... name = ... print "hello %(name)s you are %(age)s years old" % locals() Sometimes I use its cousin: def example2(obj): print "The file at %(path)s has %(length)s bytes" % obj.__dict__ I don't ne...

Django: how to include the file?

Hi! I have a Django Application. I want to have all my models to be separated in files and lay in the specific directory, for instance: /usr/project/models/myModel.py Is it any possible? Just importing through from myModel import * doesn't work, unfortunately. Is there any specific way to do this? ...

Dynamically importing modules in Python3.0?

I want to dynamically import a list of modules. I'm having a problem doing this. Python always yells out an ImportError and tells me my module doesn't exist. First I get the list of module filenames and chop off the ".py" suffixes, like so: viable_plugins = filter(is_plugin, os.listdir(plugin_dir)) viable_plugins = map(lambda name: nam...

Knight's Tour using a Neural Network

I was looking at the knights tour problem and decided to have a go at implementing it in python using a neural network to find solutions. The general explanation of the method can be found on Wikipedia While I think I have implemented it correctly (I can't see anything else that is wrong), it doesn't work, it updates a few links, remov...

Extracting text fields from HTML using Python?

Hi, what is the best way to extract data from this HTML file and put it into MySQL database with company phone number, company name and email with a primary key as phone number? </tr><tr class="tableRowOdd"> <td>"JSC company inc. 00" &lt;[email protected]&gt;</td> <td>1231231234</td> </tr><tr class="tableRowEv...