python

How to render contents of a tag in unicode in BeautifulSoup?

This is a soup from a WordPress post detail page: content = soup.body.find('div', id=re.compile('post')) title = content.h2.extract() item['title'] = unicode(title.string) item['content'] = u''.join(map(unicode, content.contents)) I want to omit the enclosing div tag when assigning item['content']. Is there any way to render all the c...

Logout or switch user in Windows using Python

I want to write a small program that given a time (in minutes) as input, sleeps in the background for that time, and then forces a return to the "switch user screen" (equivalent to the Winkey+L combination) or logs off a user (may be another user logged in on the same machine). What functions or libraries in Python could I use for this?...

Python - checking variable existing

Hello, i want to check whether variable exists. Now I'm doing something like this: try: myVar except NameError: # Doint smth Are there any other ways without exceptions? Or is that part of code right? ...

Why is django giving error: no module named django.core?

I get the error in question when I attempt to create a project. I followed the instructions found at how to install python an django in windows vista. ...

Python: get http headers from urllib call?

does urllib fetch the whole page? when a urlopen call is made? I'd like to just read the http response header without getting the page it looks like urllib opens the http connection and then subsequently gets the actual html page... or does it just start buffering the page with the url open call? import urllib2 myurl = 'http://bit.ly...

barchart (o plot) 3D in python

I need to plot some data in various forms. Currently I'm using matplotlib and I'm fairly happy with the plots I'm able to produce. This question is on how to plot the last one. The data is similar to the "distance table", like this (just bigger, my table is 128x128 and still have 3 or more number per element) Now, my data is much bette...

How does garbage collection in Python work with class methods?

class example: def exampleMethod(self): aVar = 'some string' return aVar In this example, how does garbage collection work after each call to example.exampleMethod()? Will aVar be deallocated once the method returns? ...

Using virtualenv on Mac OS X

I've been using virtualenv on Ubuntu and it rocks, so I'm trying to use it on my Mac and I'm having trouble. The virtualenv command successfully creates the directory, and easy_install gladly installs packages in it, but I can't import anything I install. It seems like sys.path isn't being set correctly: it doesn't include the virtual s...

Parse Javascript to instrument code

Hi, i need to split a javascript file into single instructions. For example: a = 2; foo() function bar() { b = 5; print("spam"); } has to be separated into three instructions. (assignment, function call and function definition). Basically i need to instrument the code, injecting code between these instructions to perform che...

Writing a __init__ function to be used in django model

Hi, I'm trying to write an init function for one of my models so that I can create an object by doing p = User('name','email') When I write the model, I have def __init__(self, name, email, house_id, password): models.Model.__init__(self) self.name = name self.email = email This works, and ...

call function between time intervals

In app engine I would like to call a function if the current time is between a particular interval. This is what I am doing now. ist_time = datetime.utcnow() + timedelta(hours=5, minutes = 30) ist_midnight = ist_time.replace(hour=0, minute=0, second=0, microsecond=0) market_open = ist_midnight + timedelta(hours=9, minutes = 55) market_...

Profiling in Python: Who called the function?

I'm profiling in Python using cProfile. I found a function that takes a lot of CPU time. How do I find out which function is calling this heavy function the most? EDIT: I'll settle for a workaround: Can I write a Python line inside that heavy function that will print the name of the function that called it? ...

How do I choose which Python installation to run in a PyObjC program?

I use Python 2.6 more than I use Leopard's default python installation, so I have it set as my main Python installation. But I'd rather use the default Python for a PyObjC program I'm working on. Is there any way to specify to only use it instead of Python 2.6? ...

list.append or list += ?

Which is more pythonic? list.append(1) or list += [1] ...

FastCgi crashes -- Want to catch all exceptions but how?

Hi, I have a django app running on apache with fastcgi (uses Flup's WSGIServer). This gets setup via dispatch.fcgi, concatenated below: #!/usr/bin/python import sys, os sys.path.insert(0, os.path.realpath('/usr/local/django_src/django')) PROJECT_PATH=os.environ['PROJECT_PATH'] sys.path.insert(0, PROJECT_PATH) os.chdir(PROJECT_PAT...

How can I check parity without converting to binary?

How can I get the number of "1"s in the binary representation of a number without actually converting and counting ? e.g. def number_of_ones(n): # do something # I want to MAKE this FASTER (computationally less complex). c = 0 while n: c += n%2 n /= 2 return c >>> number_of_ones(5) 2 >>> ...

wxPython: Drawing a vector-based image from file

How can I draw a vector-based image from a file in wxPython? I know nothing of image formats for such a thing, so please recommend. ...

Downloading a web page and all of its resource files in Python

I want to be able to download a page and all of its associated resources (images, style sheets, script files, etc) using Python. I am (somewhat) familiar with urllib2 and know how to download individual urls, but before I go and start hacking at BeautifulSoup + urllib2 I wanted to be sure that there wasn't already a Python equivalent to...

where to put method that works on a model

I'm working with Django. I have a model called Agrument. Arguments have sides and owners. I have a function that returns back the side of the most recent argument of a certain user. like obj.get_current_side(username) I've added this to the actual Argument model like this def get_current_side(self, user): return self.argu...

Python regular expression implementation details

A question that I answered got me wondering: How are regular expressions implemented in Python? What sort of efficiency guarantees are there? Is the implementation "standard", or is it subject to change? I thought that regular expressions would be implemented as DFAs, and therefore were very efficient (requiring at most one scan of the...