Archive

Posts Tagged ‘programming’

Getting reTweets from Python

September 22nd, 2010 1 comment

In this snippet I will show how to use the Tweepy library to get statistics from Tweeter.

The script will read a sample feed from the Twitter Streaming API (http://dev.twitter.com/pages/streaming_api) and perform the following actions:

* Reads the sample feed
* Notes the number of retweets seen
* Tracks the number of times posts have been retweeted, and
* Produces an hourly report of the 20 most frequently retweeted posts

Read more…

How to calculate the closest point to a segment

June 6th, 2010 No comments

Hi, below there is a little snippet that calculates the closest point to a segment. It is written in C++ but could be easily translated to any other language. Disclaimer: given its age, it use hungarian notation, and some names are in spanish, but it was deeply tested and works ok. ;)
Read more…

Making a simple html editor in C++ Builder

June 6th, 2010 2 comments

Back in time, I had to make a simple html editor. It was very easy, by using a simple TCppWebBrowser (wb) and the IHTMLDocument2 interface. You will also need a TMemo (memo1) control. Here is the source code, hope it helps someone :)
Read more…

How to replace a substring using regex in Python

June 6th, 2010 7 comments

The problem: You match a string with your regex, but you need to replace just a portion of it. How could we replace it?
The trick is simple, put the text you want to replace within “()” which means “group” in regex language. If the regex worked, you could replace just that portion by using Python match information, like in this example:

#the first group contains the expression we want to replace
pat = "word1\s(.*)\sword2"
test = "word1 will never be a word2"
repl = "replace"

import re
m = re.search(pat,test)

if m and m.groups() > 0:
  line = test[0:m.start(1)] + repl + test[m.end(1):len(test)]
  print line
else:
  print "the pattern didn't capture any text"

This will print: ‘word1 will never be a word2

The group to be replaced could be located in any position of the string.

Multiline regex pattern

June 5th, 2010 6 comments

Task: Parse a file and capture whatever text appears between a pair of double quotes like the following:

“Catch me”

Not so difficult, you could use the following regex:

“.*”

This will catch any character within double quotes in a group
¿any? Read more…