This feed contains pages tagged "python".

Twitter's recent change to oauth broke my favorite twitter client twyt. It seems that it also broke twitters android client so they've left basic auth available to any client identifying with the parameter source=twitterandroid - after the applying the patch below twyt will identify as the twitter android client and will continue to work with basic authenticatoin.

=== modified file 'twyt/twitter.py'
--- twyt/twitter.py 2009-11-19 21:48:45 +0000
+++ twyt/twitter.py 2010-09-14 08:53:28 +0000
@@ -97,6 +97,8 @@
        else:
            url = self.baseurl + handler

+       data.append(('source', 'twitterandroid'))
+
        req = None
        if method == 'GET':
            url = url + '?' + urllib.urlencode(data)
Posted Tue Sep 14 10:20:54 2010 Tags: ?patch python

Say you have a list of country names and you want to highlight those countries on a map, shaded by the number of repitions of that country in the list. You might want to go from a list like this:

locations = ['Slovenia',
         'Slovenia',
         'Italy',
         'Russian Federation',
         'Serbia',
         'United Kingdom',
         'Germany',
         'Norway',
         'Peru',
         'Poland',
         'Moldova',
         ...
         'Germany',
         'France',
         'United Kingdom']

To an image like this, from google charts:

The google charts API requires ISO 3166 country codes, pycountry can translate country names to ISO 3166 codes:

codes = [pycountry.countries.get(name=l).alpha2 for l in locations]

Counting the repitions in codes is easy:

code_count = defaultdict(int)
for code in codes:
    code_count[code] += 1

The last thing to do is save the parameters and make the charts URL:

base = 'http://chart.apis.google.com/chart?'
params = {
    'chco': 'FFFFFF,CCFFCC,88FF88,00FF00',
    'chf': 'bg,s,EAF7FE',
    'chs': '440x220',
    'cht': 't',
    'chtm': 'world'}

params['chd'] = 't:'+
                ",".join(
                str(int((counts[k]/float(max(counts.values()))*100))) 
                for k in sorted(counts.keys()))

params['chld'] = "".join(sorted(counts.keys()))

URL = base + "&".join(
            "=".join([k, params[k]) for k in params.keys())

After all that URL looks like:

http://chart.apis.google.com/chart?chd=t:60,20,20,100,40,100,60,20,20,20,20,20,20,100,20,60,60&chf=bg,s,EAF7FE&chco=FFFFFF,CCFFCC,88FF88,00FF00&chtm=world&chld=BEBYCZDEFRGBITMDNOPEPLPTRSRUSESIUA&chs=440x220&cht=t

Which is the link to the map shown above.

Posted Wed Feb 10 12:03:54 2010 Tags: ?charts ?google python

It's often useful to define element-wise operators for tuples, particularly when they are used to represent coordinate pairs. In python a simple operator factory function can be used to create these tuple operators from their scalar equivalents.

>>> import operator

>>> def tuple_operator_factory (operator):
...     def toperator (*args):
...         return tuple(map(lambda x: operator(*x), zip(*args)))
...     return toperator

>>> tsub = tuple_operator_factory(operator.sub)
>>> tsub((2,3), (1,1))
(1,2)
>>> tmul = tuple_operator_factory(operator.mul)
>>> tmul((2,2), (3,4))
(6,8)
Posted Sun Jan 17 16:38:18 2010 Tags: python ?tuple

rtmc - a command line client for Remember The Milk

rtmc is a command line client for Remember The Milk. It allows task creation, completion, deletion, prioritisation and of course viewing.

Configuration

Running rtmc for the first time will create the file ~/.rtmcrc and prompt you to populate this file with your API key and shared secret. Re-run rtmc and open the supplied URL to allow access to your account.

Commands

  • add STRING - add a new task, parsing STRING in the smart add syntax.
  • ls - show incomplete tasks, optionally filtered by:
    • --tag TAG - Only items with the tag TAG.
    • --list LIST - Only items with the list LIST.
    • --priority PRI - Only items with a priority higher than PRI.
  • complete N - complete the task numbered N.
  • delete N - delete task N.
  • priority N PRIORITY - set the priority of task N to PRIORITY.

Tags

rtmc makes local changes to the tags of a task: if the task priority is set then the priority is added to the tags, and if the task's due date is set then that too is added to the tags. The format of the date will be "Dec 2010"; tasks due this year will be formatted "25 Dec", those due this week are formatted "Thursday" and tasks due today and tomorrow are tagged as such. Tasks due in the past are tagged "Overdue".

Requirements

rtmc requires termcolor, pyrtm, and argparse, all available from the cheeseshop.

Download

You can download the latest version or clone your own with

git clone http://joe.milbourn.org.uk/clone/projects/rtmc
Posted Tue Nov 24 22:09:46 2009 Tags: ?pyrtm python ?remember the milk ?script

What to do if your long running simulation gets accidentally killed? Have it automatically recover from a checkpoint of course!

Attached is a python script which counts slowly from zero to nine:

$ python2.5 checkpoint.py
0
1
2
3
4
5
6
7
8
9

However, at every step the state of the counter is stored, so if the process dies it can be restored:

$ python2.5 checkpoint.py
0
1
2
^CTraceback (most recent call last):
  File "checkpoint.py", line 41, in <module>
    for i in checkpointed_range(10):
  File "checkpoint.py", line 22, in next
    sleep(1);
Keyboard Interrupt
$ python2.5 checkpoint.py
3
4
5
6
7
8
9

Attachments:

  1. checkpoint.py
Posted Mon Oct 19 12:44:54 2009 Tags: python