python

How to have a URL like this in Django?

How can I have URLs like example.com/category/catename-operation/ in Django? Also in some cases the user enters a space separated category, how can I handle that? E.g if the user enters the category as "my home", then the URL for this category will become example.com/my home/ which is not a valid URL. How can I handle these things? ...

Reference to Part of List - Python

If I have a list in python, how can I create a reference to part of the list? For example: myList = ["*", "*", "*", "*", "*", "*", "*", "*", "*"] listPart = myList[0:7:3] #This makes a new list, which is not what I want myList[0] = "1" listPart[0] "1" Is this possible and if so how would I code it? Cheers, Joe ...

How do I remove something form a list, plus string matching?

[(',', 52), ('news', 15), ('.', 11), ('bbc', 8), ('and', 8), ('the', 8), (':', 6), ('music', 5), ('-', 5), ('blog', 4), ('world', 4), ('asia', 4), ('international', 4), ('on', 4), ('itunes', 4), ('online', 4), ('digital', 3)] Suppose I have this list, with tuples inside. How do I go through the list and remove elemen...

python inheritance and __init__ functions

I came across the folloqing type of code when looking for some pyQt examples : class DisplayPage(QWizardPage): def __init__(self, *args): apply(QWizardPage.__init__, (self, ) + args) What does *args mean ? What is the purpose of using apply for this type of code ? ...

I am trying to determine if a string is a Question. How can I analyze the "?" symbol (python)

This is a question: "Where is the car?" This is NOT a question: "Check this out: http://domain.com/?q=test" How do I write a function to analyze a string so that we know for sure it is a question and not part of a URL? ...

Trying to get a terminal to work in Emacs...

Hi so, I've been having a lot of problems with emacs and trying to get the terminal to work with: M-x term I installed cygwin and I fixed up my .emacs to include the paths: (setenv "PATH" (concat "c:/cygwin/bin;" (getenv "PATH"))) (setq exec-path (cons "c:/cygwin/bin" exec-path)) (require 'cygwin-mount) (cygwin-mo...

How to get the next token (int, float or string) from a file in Python?

Is there some way to just get the next token from a file in Python, as for example the Scanner class does in Java? File file = new File("something"); Scanner in = new Scanner(file); double a = in.nextDouble(); String s = in.next(); I'd like to ignore whitespaces, tabs, newlines and just get the next int/float/word from the file. I kno...

Django, Import tables as models

I would like to know if it's possible to use django over existing db tables that defines the models. Instead of defining models in order to create db tables ...

Clustering text in Python

I need to cluster some text documents and have been researching various options. It looks like LingPipe can cluster plain text without prior conversion (to vector space etc), but it's the only tool I've seen that explicitly claims to work on strings. Are there any Python tools that can cluster text directly? If not, what's the best wa...

Django, is possible to run two different versions?

Hello, I have a server on which I have two sites built with Django and Python, one site is major site is build with an older version of django, the other with the newer release, I have upgraded to the new release and major aspects of my other site have broken, is it possible to tell the site to use a different version in say the pytho...

How to parse *.py file with python?

How to parse *.py file with python? If my question is duplicated, please let me know another thread, because I can't find it. Why I like to do is, I like to try Basic Source Code Converter from Python to Go Language. Which module should I use? And your Idea on, should I go ahead or better not? If I should go ahead, please let me know...

How to find number of bytes taken by python variable

Is there anyway i can know how much bytes taken by particular variable in python. E.g; lets say i have int = 12 print (type(int)) it will print <class 'int'> But i wanted to know how many bytes it has taken on memory? is it possible? ...

How do I make django's markdown filter transform a carriage return to <br />?

How can I change the default behavior in the markdown filter so that it transforms a newline to a br tag? ...

Adding Version Control / Numbering (?) to Python Project

With my Java projects at present, I have full version control by declaring it as a Maven project. However I now have a Python project that I'm about to tag 0.2.0 which has no version control. Therefore should I come accross this code at a later date, I won't no what version it is. How do I add version control to a Python project, in th...

How to apply a logical operator to all elements in a python list

I have a list of booleans in python. I want to AND (or OR or NOT) them and get the result. The following code works but is not very pythonic. def apply_and(alist): if len(alist) > 1: return alist[0] and apply_and(alist[1:]) else: return alist[0] Any suggestions on how to make it more pythonic appreciated. ...

Running average in Python

Is there a pythonic way to build up a list that contains a running average of some function? After reading a fun little piece about Martians, black boxes, and the Cauchy distribution, I thought it would be fun to calculate a running average of the Cauchy distribution myself: import math import random def cauchy(location, scale): ...

Python: parsing date with timezone from an email

I am trying to retrieve date from an email. At first it's easy: message = email.parser.Parser().parse(file) date = message['Date'] print date and I receive: 'Mon, 16 Nov 2009 13:32:02 +0100' But I need a nice datetime object, so I use: datetime.strptime('Mon, 16 Nov 2009 13:32:02 +0100', '%a, %d %b %Y %H:%M:%S %Z') which raise V...

Compact Class DSL in python

I want to have compact class based python DSLs in the following form: class MyClass(Static): z = 3 def _init_(cls, x=0): cls._x = x def set_x(cls, x): cls._x = x def print_x_plus_z(cls): print cls._x + cls.z @property def x(cls): return cls._x class MyOtherClass(MyClass): z...

Matching multiple regex groups and removing them

I have been given a file that I would like to extract the useful data from. The format of the file goes something like this: LINE: 1 TOKENKIND: somedata TOKENKIND: somedata LINE: 2 TOKENKIND: somedata LINE: 3 etc... What I would like to do is remove LINE: and the line number as well as TOKENKIND: so I am just left with a string that ...

Genshi: Nested for loops

I need to generate a HTML using a Genshi template. The Html is, basicaly a very long html with tables. The data comes in a simple CSV, so, i read it with python, i put it into a list[] and then i call the template and send the variable (the list) Actually i solved it by doing something like this in the template: <html> <?python> fo...