python

Update a gallery webpage via Dropbox?

I'd like to know if the following situation and scripts are at all possible: I'm looking to have a photo-gallery (Javascript) webpage that will display in order of the latest added to the Dropbox folder (PHP or Python?). That is, when someone adds a picture to the Dropbox folder, there is a script on the webpage that will check the D...

Python: List comprehension to assign different values

I'm making a 2D list and I would like to initialize it with a list comprehension. I would like it to do something like this: [[x for i in range(3) if j <= 1: x=1 else x=2] for j in range(3)] so it should return something like: [[1,1,1], [1,1,1], [2,2,2]] How might I go about doing this? Thanks for your help. ...

python sax: is there a way to halt the parsing from inside a content handler?

Is there a way to halt the parsing from inside a content handler? Or is throwing an exception the only way? Note that I am using xml.sax.parseString. ...

How do I do this in Python? List to function

def getStuff(x): return 'stuff'+x def getData(x): return 'data'+x thefunctions = [] thefunctions.append("getStuff") thefunctions.append("getData") for i in thefunctions: print i('abc') Is this possible? Thank you. ...

Elegant way to skip first line when using python fileinput module?

Is there an elegant way of skipping first line of file when using python fileinput module? I have data file with nicely formated data but the first line is header. Using fileinput I would have to include check and discard line if the line does not seem to contain data. The problem is that it would apply the same check for the rest of t...

Python: What is the common header format?

I came across the following header format for Python source files in a document about Python coding guidelines: #!/usr/bin/env python """Foobar.py: Description of what foobar does.""" __author__ = "Barack Obama" __copyright__ = "Copyright 2009, Planet Earth" Is this the standard format of headers in the Python world? What oth...

Binary numbers in Python

How can I add, subtract, and compare binary numbers in Python without converting to decimal? ...

.vimrc configuration for Python

My current .vimrc configuration is below: set nohlsearch set ai set bg=dark set showmatch highlight SpecialKey ctermfg=DarkGray set listchars=tab:>-,trail:~ set list autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class set tabstop=4 set shiftwidth=4 set expandtab set autoindent set smartinden...

Python module search path problem

Hi, I am trying to work on a dev environment but am find problems in that python seems to be using modules from the site-packages directory. I want it to be using the modules from my dev directory. sys.path returns a bunch of dirs, like this ['', '/usr/lib/python26.zip', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib...

BioPython: Skipping over bad GIDs with Entrez.esummary/Entrez.read

Sorry about the odd title. I am using eSearch & eSummary to go from Accession Number --> gID --> TaxID Assume that 'accessions' is a list of 20 accession numbers (I do 20 at a time because that's the maximum that NCBI will allow). I do: handle = Entrez.esearch(db="nucleotide", rettype="xml", term=accessions) record = Entrez.read(ha...

redirect() Not Actually Redirecting (Routes for Pylons)

I am constructing a table of routes in my Pylons app. I can't get redirect() to work the way that it should work. I'm not sure what I'm doing wrong. Here is an example of redirect() usage from the Routes documentation: map.redirect("/home/index", "/", _redirect_code="301 Moved Permanently") Here is what appears in my routing.py f...

How to disable URL redirection in Python when using M2Crypto SSL?

This is what my code looks like: url_object = urlparse(url) hostname = url_object.hostname port = url_object.port uri = url_object.path if url_object.path else '/' ctx = SSL.Context() if ctx.load_verify_locations(cafile='ca-bundle.crt') != 1: raise Exception("Could not load CA certificates.") ctx.set_verify(SSL.verify_peer | SSL.verify...

How to print a list in Python "nicely"

In PHP, I can do this: echo '<pre>' print_r($array); echo '</pre>' In Python, I currently just do this: print the_list However, this will cause a big jumbo of data. Is there any way to print it nicely into a readable tree? (with indents)? ...

An ALGORITHM to create one list from "X" nested lists in Python.

What is the simplest way to create this list in Python? First, suppose I have this nested list: oldList = [ [{'letter':'a'}], [{'letter':'b'}], [{'letter':'c'}] ] I want a function to spit out: newList = [ {'letter':a}, {'letter':'b'}, {'letter':'c'} ] Well, this could be done manually. However, what if there are three nested? ......

Werkzeug in General, and in Python 3.1

I've been looking really hard at all of the way**(s)** one can develop web applications using Python. For reference, we are using RHEL 64bit, apache, mod_wsgi. History: PHP + MySQL years ago PHP + Python 2.x + MySQL recently and current Python + PostgreSQL working on it We use a great library for communicating between PHP and Pytho...

Reasons to use distutils when packaging C/Python project

I have an open source project containing both Python and C code. I'm wondering that is there any use for distutils for me, because I'm planning to do a ubuntu/debian package. The C code is not something that I could or want to use as Python extension. C and Python programs communicate with TCP/IP through localhost. So the bottom line he...

Convert a decimal matrix to a binary matrix in SciPy

Suppose I have a integer matrix which represents who has emailed whom and how many times. For social network analysis I'd like to make a simple undirected graph. So I need to convert the matrix to binary matrix and later into a list of tuples. My question: is there a fast, convenient way to reduce the decimal matrix to a binary matrix. ...

How to print a list more nicely?

This is similar to http://stackoverflow.com/questions/1523660/how-to-print-a-list-in-python-nicely, but I would like to print the list even more nicely -- without the brackets and apostrophes and commas, and even better in columns. foolist = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf', 'pdcurses-devel', 'msvcr...

Does anyone know a way to scramble the elements in a list?

thelist = ['a','b','c','d'] What's the best way to scramble them in Python? ...

How to have an error but continue the script in python?

Suppose I have this code in Python: l = dict['link'] t = dict['title'] <<<<<<<<error here, there is no "title" d = dict['description'] k = dict['keyword'] What if there is an error on line 2, but I want it to continue running the script and assign the other values? Can I just "ignore" the errors? EDIT: I know how to do a simp...