python

Problem related to finding permuations and combinations using Python

I have 2 variables - a and b. I need to fill up k places using these variables. So if k = 3 output should be [a,a,a], [a,a,b] , [a,b,a], [b,a,a], [a,b,b], [b,a,b], [b,b,a] and [b,b,b] Input - k Output - All the combinations How do I code this in Python? Can itertools be of any help here? ...

How can I add media (js and css) to my modelformset in Django?

Hey, I'm trying to add an Export CSV function to Django Admin. So far I've managed the following: 1. Added an 'Export CSV' link to the admin change_list.html. 2. On the export page, I start off by creating the required model from two separate parts of the URL using model = get_model(app_name, module_name). 3. From this model I first cre...

beautiful soup not parsing site

i started this script in Calibre. when i found out that Calibre can not do what i want, i installed spyder and i am now in the process of making it real python. i am trying (at this stage) to get a list of url from an index. in the new urls, i want to get other urls (sort of an index of indexs) in order to get to the 2ns level index i n...

how to insert text inside bar if bar color equal to purple

Hi guys! So I've this image : What I trying to do if to leave 'H37Rv' only in the purple bar. My code is the following: rects = ax.bar(ind, num, width, color=colors) for rect in rects: height = int(rect.get_height()) if height < 5: yloc = height + 2 clr = '#182866' else: ...

Slice notation in scala ?

Is there something similar to slice notation in python in scala ? I think this is really a useful operation that should be incorporated in all languages. ...

Number of matches in regex substitution

I am looking for a Pythonic way to simplify this code: fix = re.compile(r'((?<=>\n)(\t){2}(?=<))') fixed_output = re.sub(fix, 1*2*' ', fixed_output) fix = re.compile(r'((?<=>\n)(\t){3}(?=<))') fixed_output = re.sub(fix, 2*2*' ', fixed_output) # and so on... That is: if there are n tab characters between ">" and "<", they are replaced ...

Best practices with Facebook Connect logout?

Hi folks, I'm building a web app with Django using Facebook authentication. Now I have encountered a problem with the logout. Do developers usually hide the default logout link? It is quite tricky coming up with the logic of implementing a logout while a user is connected to a facebook acount, which is still logged in. Any ideas?...

Calling staticmethod inside class level containers intialization

Given the following example class: class Foo: def aStaticMethod(): return "aStaticMethod" aVariable = staticmethod(aStaticMethod) aTuple = (staticmethod(aStaticMethod),) aList = [staticmethod(aStaticMethod)] print Foo.aVariable() print Foo.aTuple[0]() print Foo.aList[0]() Why would the call to aVariable work...

python nose framework: A plugin to display results in a human friendly format

Any format which is targeted for humans (.html, .doc, whatever) would be good. I cannot find any plugin that provides it All I found was XUNIT or XML output.. ...

What is the difference between None check and var or default syntax in Python?

I noted this syntax listed as a gotchya but with no explanation as to why: def func(x=None): #good if x == None: x = [] #bad x = x or [] In what ways can this be a gotchya? ...

regex to parse tables wrapped into xml

Suppose we have a table: Key|Val|Flag 01 |AAA| Y 02 |BBB| N ... wrapped into xml this way: <Data> <R><F>Key</F><F>Val</F><F>Flag</F></R> <R><F>01</F><F>AAA</F><F>Y</F></R> <R><F>02</F><F>BBB</F><F>N</F></R> ... </Data> There can be more columns and rows, obviously. Now I'd like to parse XML back to table using single regex...

how to declare variable type, C style in python

Hi, i'm a programming student and my teacher is starting with C to teach us the programming paradigms, he said it's ok if i deliver my homework in python(it's easier and faster for the homeworks). And i would like to have my code to be as close as posible as in plain C. Question is How do i declare data types for variables in python like...

In Python, why do we need readlines() when we can iterate over the file handle itself?

In Python, after fh = open('file.txt') one may do the following to iterate over lines: for l in fh: pass Then why do we have fh.readlines()? ...

Os Check And prompt proper if

Can python see which windows i use e.x. windows 7 windows xp windows vista and if windows vista print you use windows vista, or execute other command ...

how to create .ASPXAUTH cookie on python

hi; i need to create .ASPXAUTH cookie on python. i programing to desktop client. and first request not need .ASPXAUTH cookie but second request is need. My First Request Headers: User-Agent: WebPolicy Host: xxx.host Cache-Control: no-cache My First Response Headers: reply: 'HTTP/1.1 200 OK\r\n' header: Cache-Control: private hea...

accessing python dictionary

I am writing code that will search twitter for key words and store them in a python dictionary: base_url = 'http://search.twitter.com/search.json?rpp=100&amp;q=4sq.com/' query = '7bOHRP' url_string = base_url + query logging.info("url string = " + url_string) json_text = fetch(url_string) ...

NaN giving an error depending on Python startup?

Hi, I am using Python4Delphi to embed Python in a Delphi program. Versions: Python 2.6.4, Delphi 2009, Windows XP. The Delphi program crashes with EInvalidOp when importing json. I tracked it to the line NaN, PosInf, NegInf = float('nan'), float('inf'), float('-inf') in json.decoder. Sure enough, the command float('nan') raises an ...

Django: most efficient way to query many records?

Hello, I have a table with a few thousands records (products). Each product has a 4 different categories: CAT1 CAT2 CAT3 CAT4 I wonder if is there a method, or what is the best practice, to dynamically retrive the available categories based on the categories already selected (using Ajax). Example: if CAT1 = green all the products wit...

What is the most general python type to which I can add attributes?

Hi, I have a class Foo with a method isValid. Then I have a method bar() that receives a Foo object and whose behavior depends on whether it is valid or not. For testing this, I wanted to pass some object to bar whose isValid method returns always False. For other reasons, I cannot create an object of Foo at the time of testing, so I ...

Best Practices for Python UnicodeDecodeError

I use Pylons framework, Mako template for a web based application. I wasn't really bother too deep into the way python handles the unicode strings. I had tense moment when I did see my site crash when the page is rendered and later I came to know that it was related to Unicode Decode error http://wiki.python.org/moin/UnicodeDecodeError ...