python

Given a list of slices, how do I split a sequence by them?

Given a list of slices, how do I separate a sequence based on them? I have long amino-acid strings that I would like to split based on start-stop values in a list. An example is probably the most clear way of explaining it: str = "MSEPAGDVRQNPCGSKAC" split_points = [[1,3], [7,10], [12,13]] output >> ['M', '(SEP)', 'AGD', '(VRQN)', 'P'...

Find a file in python

I have a file that may be in a different place on each user's machine. Is there a way to implement a search for the file? A way that I can pass the file's name and the directory tree to search in? ...

How do I strip a character in Django admin after submission before sending value to dB?

I'm more comfortable with PHP & MySQL, don't know a lick of Python (yet) except for the teeny tiny bit I picked up using the Django admin recently...therefore please excuse this very stupid question that I'm embarrassed to ask. In PHP this would be trivial for me... I'm using a color picker (farbtastic) w/ Django Admin, it's the only on...

Lxml html xpath context

Hello, I'm using lxml to parse a HTML file and I'd like to know how can I set the context of xpath search. What I mean I that I have a node element and want to make xpath search only inside this node as if it was the root one. For example, I have a form node and xpath search //input return only inputs of the given form as opposed to all ...

Python Unit Tests - Am I using SetUp wrong?

What am I doing wrong here? import unittest class Test_1(unittest.TestCase): def SetUp(self): self.data = [] def test_data(self): self.assertEqual(len(self.data),0) if __name__=='__main__': unittest.main() When I run it, it says: Traceback (most recent call last): File "C:...\break_unit_test.py", ...

Can a python module have a __repr__?

Can a python module have a __repr__? The idea would be to do something like: import mymodule print mymodule EDIT: precision: I mean a user-defined repr! ...

How to check if a list is empty in Python?

The API I'm working with can return empty [] lists. What isn't working: if myList is not None: #not working if myList is not []: #not working What will work? ...

Working with JSON in Python 2.6?

I'm really new to Python, but I've picked a problem that actually pertains to work and I think as I figure out how to do it I'll learn along the way. I have a directory full of JSON-formatted files. I've gotten as far as importing everything in the directory into a list, and iterating through the list to do a simple print that verifies ...

time.sleep and suspend (ie. standby and hibernate)

For example, if I do time.sleep(100) and immediately hibernate my computer for 99 seconds, will the next statement be executed in 1 second or 100 seconds after waking up? If the answer is 1 second, how do you "sleep" 100 seconds, regardless of the length of hibernate/standby? ...

Get functions called in a Python expression

I have a database that holds the name of Python functions and a string for their code. I want the user to be able to enter a Python code and see the result. The problem is that I need to know the names of the functions they call in order to retrieve the code from the database. For instance, if they enter cubic_fit(1, 2, get_data()), I ne...

In Python, how do I use urllib to see if a website is 404 or 200?

How to get the code of the headers through urllib? ...

python stdin eof

Hi all, How to pass python eof to stdin here is my code p = Popen(commd,stdout=PIPE,stderr=PIPE,stdin=PIPE) o = p.communicate(inputstring)[0] when i run the commd in command line after i input the inputstring windows still expecting a Ctrl+Z to finish accepting input. How can I pass eof or Ctrl+Z in program? Thanks! ...

How should unit tests be documented?

I'm trying to improve the number and quality of tests in my Python projects. One of the the difficulties I've encountered as the number of tests increase is knowing what each test does and how it's supposed to help spot problems. I know that part of keeping track of tests is better unit test names (which has been addressed elsewhere), bu...

Using keys with spaces

Is there a way to do something like the following in Django templates? {% for hop in hops%} <tr> <td>{{ hop.name }}</td> <td>{{ hop.mass }}</td> <td>{{ hop."boil time" }}</td> </tr> {% endfor %} The hop."boil time" doesn't work. The simple solution is rename the key boil_time, but I'm interested in alt...

programmatically determine if an evaluation of a function is being assigned?

I would like to do this: def foo(): if <a magical condition>: return x else: poof() # or... def foo(): x = <a magical object> return x def poof(): print 'poof!' bar = foo() # bar points to <a magical object> but poof() is not called foo() # prints 'poof!' I guess it comes down to what the circu...

Unzip part of a file using python gzip module

Hi, I am trying to unzip a gzipped file in Python using the gzip module. The pre-condition is that, I get 160 bytesof data at a time, and I need to unzip it before I request for the next 160 bytes. Partial unzipping is OK, before requesting the next 160 bytes. The code I have is import gzip import time import StringIO file = open('inp...

Returning array of data mapping values to parameters in python

I have a few functions that return an array of data corresponding to parameters ranges. Example: for a 2d array a, the a_{ij} value corresponds to the parameter set (param1_i, param2_j). How do I return the result and keep the parameter-value correspondence? Calling the function for each and every of param1_i, para2_j and returning on...

Sorting a 3 parallel list that includes strings and numeric value in Python

how to sort using 3 parallel array lists: num1 = ['a','b','c,'] num2 = ['apple','pear','grapes'] num3 = [2.5,4.0,.68] I used 2 for statements followed by a if statement. Sorting by elements the output should be: a apple 2.5 b pear 4.0 c grapes .68 unfortunately, I am having issues with sorting the 3rd n...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single instances? I gotta say I only recently started getting into Python but its making this project so much easier. I'm just a bit stumped on this kin...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and must be fired after restarting. Jobs must enter and exit the scheduler in a transaction (i.e. if some database operation fail, in a databas...