python

Python script to print all function definitions of a C/C++ file

I want a python script to print list of all functions defined in a C/C++ file. e.g. abc.c defines two functions as: void func1() { } int func2(int i) { printf("%d", i); return 1; } I just want to search the file (abc.c) and print all the functions defined in it (function names only). In the example above, I would like to print func1,...

Python RegEx skipping the first few characters?

Hey I have a fairly basic question about regular expressions. I want to just return the text inside (and including) the body tags, and I know the following isn't right because it'll also match all the characters before the opening body tag. I was wondering how you would go about skipping those? x = re.match('(.*<body).*?(</body>)', file...

place labels between ticks

in matplotlib, how do i place ticks labels between ticks (not below ticks) for example: when plotting a the stock price over time i would like the x axis minor ticks to display months and the years to show up between consecutive x axis major ticks (not just below the major ticks) ---|---|---|---|---|---|---|---|---|---|---|---|---|---|...

Determining neighbours of cell two dimensional list

I have a list of lists, something like [[1, 2, 3,],[4, 5, 6,],[7, 8, 9]]. Represented graphically as: 1 2 3 4 5 6 7 8 9 I'm looking for an elegant approach to checking the value of neighbours of a cell, horizontally, vertically and diagonally. For instance, the neighbours of [0][2] are [0][1], [1][1] and [1][2] or the numbers 2, 5...

Nesting generator expressions in the argument list for a python function call

I like to use the following idiom for combining lists together, sometimes: >>> list(itertools.chain(*[[(e, n) for e in l] for n, l in (('a', [1,2]),('b',[3,4]))])) [(1, 'a'), (2, 'a'), (3, 'b'), (4, 'b')] (I know there are easier ways to get this particular result, but it comes comes in handy when you want to iterate over the elements...

Problem declaring global variable in python/GAE

Hi, I'm new to python and I'm trying to obtain a username from the UI and query the result to get the phone book contacts of the user. But I am not able to set the user name to be a global variable to use it for multiple queries. Here's the code, I believe I am doing some syntax error/improper usage, please help out in correcting my cod...

dynamically adding functions to a Python module

Our framework requires wrapping certain functions in some ugly boilerplate code: def prefix_myname_suffix(obj): def actual(): print 'hello world' obj.register(actual) return obj I figured this might be simplified with a decorator: @register def myname(): print 'hello world' However, that turned out to be rat...

how to extract some text by use lxml?

hello. i want to extract some text in certain website. here is web address what i want to extract some text to make scraper. http://news.search.naver.com/search.naver?sm=tab%5Fhty&amp;where=news&amp;query=times&amp;x=0&amp;y=0 in this page, i want to extract some text with subject and content field separately. for example,if you open tha...

How do I display real-time python script output on a website?

I have a Python script that outputs something every second or two, but takes a long while to finish completely. I want to set up a website such that someone can directly invoke the script, and the output is sent to the screen while the script is running. I don't want the user to wait until the script finishes completely, because then a...

How can I strip comments and doc strings from python source code?

Is there a program which I can run like this: py2py.py < orig.py > smaller.py Where orig.py contains python source code with comments and doc strings, and smaller.py contains identical, runnable source code but without the comments and doc strings? Code which originally looked like this: #/usr/bin/python """Do something blah blah......

Python Selenium handling Timeout Exceptions with a long list of URLs

Hi, I am using selenium RC to cycle through a long list of URLs, sequentially writing the HTML from each URL to a csv file. Problem: the program frequently exits at various points in list due to URL "Timed out after 30000ms" exceptions. Instead of stopping the program when it hits a URL time-out, I was trying to have the program simply ...

Finding and adding twitter users?

Any suggestions for a good twitter library (preferably in Ruby or Python)? I have a list of usernames, and I need to be able to programmatically follow these users. I tried twitter4r in Ruby, but finding users doesn't seem to work. When I do twitter = Twitter::Client.new(:login => 'mylogin', :password => 'mypassword') user = Twitter::...

Is there a way to split a string by every nth seperator in Python?

For example, if I had the following string: "this-is-a-string" Could I split it by every 2nd "-" rather than every "-" so that it returns two values ("this-is" and "a-string") rather than returning four? ...

python package import fails

I've got a trac installation which works correctly from the command line. I deployed the trac.cgi to the proper directory, but when I open the page, I get: Trac detected an internal error: No module named pkg_resources Traceback (most recent call last): File "/some/path/htdocs/trac.cgi", line 22, in ? import pkg_resources ImportE...

Find Monday's date with Python

How do I find the previous Monday's date, based off of the current date using Python? I thought maybe I could use: datetime.weekday() to do it, but I am getting stuck. I basically want to find today's date and Mondays date to construct a date range query in django using: created__range=(start_date, end_date). ...

White listing certain HTML tags in python?

Let's say allowed_bits = ['a', 'p'] re.compile(r'<(%s)[^>]*(/>|.*?</\1>)' % ('|'.join(allowed_bits))) matches: <a href="blah blah">blah</a> <p /> and not: <html>blah blah blah</html> What I want to do is turn it on its head, so that it matches <html>blah blah</html> <script type="text/javascript">blah blah</script> and not: ...

How do I trim the number of words in Python?

For example... s = 'The fox jumped over a big brown log.' k = FUNCTION(s,4) k should be... "The fox jumped over" I can write my own function that splits on whitespace, cuts the list and then joins the list. But that is too much work. Anyone else knows a simpler way? ...

Twisted: source IP address for outbound connections

I'm in the process of implementing a service -- written in Python with the Twisted framework, running on Debian GNU/Linux -- that checks the availability of SIP servers. For this I use the OPTIONS method (a SIP protocol feature), as this seems to be a commonplace practice. In order to construct correct and RFC compliant headers, I need...

what exactly is a "register machine" ?

From http://code.google.com/p/unladen-swallow/wiki/ProjectPlan I quote: "Using a JIT will also allow us to move Python from a stack-based machine to a register machine, which has been shown to improve performance in other similar languages (Ierusalimschy et al, 2005; Shi et al, 2005)." In college I built a simple compiler for a languag...

Overriding set methods in Python

I want to create a custom set that will automatically convert objects into a different form for storage in the set (see Using a Python Dictionary as a key non-nested) for background. If I override add, remove, __contains__, __str__, update, __iter__ will that be sufficient to make the other operations behave properly, or do I need to o...