Snippets

Revision 7 as of 2009-11-26 19:31:58

Clear message

   1 #how to write text to a file (not safe)
   2 open('foo.txt', 'w').write('hello')
   3 
   4 #how to loop a certain number (e.g. 10) of times
   5 for x in range(10):
   6     print x
   7 
   8 #how to concatenate a number and a string
   9 x = 1
  10 print str(x) + " is a string"
  11 print "%s is a string" % x
  12 
  13 #how to test if a string contains another string
  14 my_string = "abcdef"
  15 if "abc" in my_string:
  16     has_abc = True
  17 
  18 #check if an item is in a list or dictionary or tuple or set
  19 my_list = [1, 2, 3, 4, 5]
  20 if 1 in my_list:
  21     has_one = True
  22 
  23 my_dict = {"a": 1, "b": 2, "c": 3}
  24 if "a" in my_dict:
  25    has_a = True
  26 
  27 # Lambda (anonymous functions)
  28 add = lambda x, y: x + y
  29 print add(3, 4) # prints "7"
  30 
  31 # Reduce
  32 from functools import reduce
  33 def factorial(x):
  34     return reduce(lambda accum, x: accum * x, range(1, x + 1), 1)
  35 
  36 # Non-destructive sort on key
  37 people = [ ... list of people objects ... ]
  38 people_by_age = sorted(people, key=lambda person: person.age)
  39 
  40 # Ternary expression (i.e. "BOOLEAN ? IF_TRUE : IF_FALSE")
  41 print "a" if x == 1 else "b"
  42 # Python 2.6 only!
  43 # 2.5 or earlier: print (x == 1) and "a" or "b"
  44 
  45 
  46 # Printing stuff nicely.
  47 from pprint import pprint
  48 pprint('Complex thing')
  49 
  50 
  51 # generators/iteration
  52 def generate_stuff ():
  53     yield "x"
  54     yield "y"
  55     yield "z"
  56 
  57 for item in generate_stuff ():
  58     print item
  59 
  60 
  61 #how to test if a string begins or ends with another string
  62 my_string = "abcdef"
  63 if my_string.startswith("abc"):
  64     starts_with_abc = True
  65 if my string.endswith("def"):
  66     ends_with_def = True
  67 
  68 #how to catch exceptions
  69 grades = ['A', 'A-', 'B', 'C']
  70 try:
  71     grades.index("F")
  72 except ValueError, inst:
  73     #print out error info
  74     print inst.args
  75 
  76 # Catching multiple exceptions in Python.
  77 try:
  78     "do something"
  79 except (ExceptionOne, ExceptionTwo):
  80     # Note: The parens are important.
  81     "handle the exception"
  82 
  83 # Convert a file url to a file path in a shell script
  84 deurledfile = `python -c 'import sys, urlparse, urllib; print urllib.unquote(urlparse.urlparse(sys.argv[1]).path)' $1`
  85 
  86 # change/update value in a file with "="
  87 try:
  88     fin = file('file1', 'r')
  89     fout = file(fin.name + '.new', 'w')
  90     for line in fin:
  91         fields = line.split('=') # Separate variable from value
  92         if "keyname" == fields[0].strip():
  93             line = "%s='%s'\n" % (fields[0], "newvalue")
  94         fout.write(line)
  95     fout.close()
  96     fin.close()
  97     os.rename(fout.name, fin.name)
  98 except (OSError, IOError), e:
  99     print("can't update the file")
 100 
 101 # how to make money in open source
 102 1. get developers
 103 2. bzr commit
 104 3.
 105 4. profit
 106 
 107 # how to generate pretty charts
 108 # http://yokozar.org/blog/archives/48
 109 import cairoplot
 110 
 111 # Appears at the top of the chart produced at the end
 112 chartTitle = "Simulated model of Wine development"
 113 chartData = {"Working Apps" : [], "Happy Users" : []}
 114 for x in xrange(100):
 115     chartData["Working Apps"].append(float(x))
 116     chartData["Happy Users"].append(float(x))
 117     x_labels = [
 118         "Project start", "25% bugs solved", "50% bugs solved",
 119         "75% bugs solved", "All bugs solved"]
 120     y_labels = ["0%", "25%", "50%", "75%", "100%"]
 121     cairoplot.dot_line_plot(
 122         "wine-model-results", chartData, 600, 600, x_labels = x_labels,
 123         y_labels = y_labels, axis = True, grid = True,
 124         x_title = chartTitle, y_title = "Percentage", series_legend=True )
 125 
 126 
 127 
 128 #how to play a sound
 129 import gst
 130 player = gst.element_factory_make("playbin", "player")
 131 player.set_property("uri", "file:///usr/share/sounds/ubuntu/stereo/bell.ogg")
 132 player.set_state(gst.STATE_PLAYING)
 133 # or
 134 import pygame
 135 pygame.mixer.init()
 136 pygame.mixer.music.load("music.ogg")
 137 pygame.mixer.music.play()
 138 
 139 
 140 #notify-osd
 141 import pynotify
 142 
 143 n = pynotify.Notification("message name", "message", "icon")
 144 n.show()
 145 
 146 # more notify-osd stuff
 147 
 148 # get the output of a command
 149 import subprocess
 150 
 151 date = subprocess.Popen(['date', '+%c'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 152 (out, err) = date.communicate()
 153 print 'exit code', date.returncode
 154 
 155 
 156 #open a link in a web browser
 157 import webbrowser
 158 
 159 url = 'http://google.com/'
 160 webbrowser.open_new(url)
 161 
 162 #get the text for an html page from the web
 163 import urllib
 164 f = urllib.urlopen(day_url)
 165 html = f.read()
 166 
 167 #create a stock cancel button and pack it into the vbox
 168 cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL)
 169 cancel_button.show()
 170 cancel_button.connect("clicked", on_cancel_clicked)
 171 
 172 
 173 # Parse XML file
 174 import xml.dom
 175 try:
 176     dom = xml.dom.minidom.parseString(xml_content)
 177 except ExpatError, e:
 178     print >> sys.stderr, 'Invalid XML:', e
 179 
 180 for p_node in dom.getElementsByTagName('p'):
 181     print 'class of paragraph', p.attributes['class']
 182     for child in p_node.childNodes:
 183         print 'XML child node:', child
 184 
 185 
 186 # search a file for a pattern and do something with every match
 187 import re
 188 for m in re.finditer('bug (\d+) blabla', text):
 189     print 'bug number': m.match(1)
 190 
 191 
 192 # download an URL and process it line by line
 193 import urllib
 194 for line in urllib.open('http://foo'):
 195     print line
 196 
 197 
 198 # grep all versioned files in a Bazaar tree
 199 # Best command ever
 200 bzr ls -VR --kind=file --null | xargs -0 grep -In <REGEX>
 201 git grep <REGEX>
 202 
 203 
 204 # Creating a Gtk+ TreeView:
 205 
 206 # Instantiate the tree store and specify the data types
 207 store = gtk.TreeStore(str, gtk.gdk.Pixbuf, int, bool)
 208 
 209 # Create a TreeViewColumn
 210 col = gtk.TreeViewColumn("File")
 211 # Create a column cell to display text
 212 col_cell_text = gtk.CellRendererText()
 213 # Create a column cell to display an image
 214 col_cell_img = gtk.CellRendererPixbuf()
 215 # Add the cells to the column
 216 col.pack_start(col_cell_img, False)
 217 col.pack_start(col_cell_text, True)
 218 # Bind the text cell to column 0 of the tree's model
 219 col.add_attribute(col_cell_text, "text", 0)
 220 # Bind the image cell to column 1 of the tree's model
 221 col.add_attribute(col_cell_img, "pixbuf", 1)
 222 
 223 col2 = gtk.TreeViewColumn("Size")
 224 col2_cell_text = gtk.CellRendererText()
 225 col2.pack_start(col2_cell_text)
 226 col2.add_attribute(col2_cell_text, "text", 2)
 227 
 228 # Create the TreeView and set our data store as the model
 229 tree = gtk.TreeView(store)
 230 # Append the columns to the TreeView
 231 tree.append_column(col)
 232 tree.append_column(col2)
 233 
 234 #make a new ListStore when columns are calculated at runtim
 235 col_count = 100
 236 self.list_store = gtk.ListStore(
 237     *[gobject.TYPE_STRING for i in range(col_count)])
 238 
 239 
 240 #Put data into desktop CouchDB:
 241 
 242 from desktopcouch.records.server import CouchDatabase
 243 from desktopcouch.records.record import Record as CouchRecord
 244 
 245 db = CouchDatabase("dbname", create=True)
 246 record = CouchRecord(
 247     {"a": 1, "b": 2}, record_type="http://recordtypeurl",
 248     record_id=XXX)
 249 db.put_record(record)
 250 
 251 # Call a desktop CouchDB view:
 252 
 253 view = """function(doc) { if (doc) emit(doc._id, doc); }"""
 254 if not self.messages.view_exists("myview", "myapp"):
 255     self.messages.add_view("myview", view, None, "myapp")
 256 result = self.messages.execute_view("myview", "myapp")
 257 
 258 #Creating a new Tomboy note
 259 def create_tomboy_note(text):
 260     bus = dbus.SessionBus()
 261     obj = bus.get_object("org.gnome.Tomboy", "/org/gnome/Tomboy/RemoteControl")
 262     tomboy = dbus.Interface(obj, "org.gnome.Tomboy.RemoteControl")
 263 
 264     n = tomboy.CreateNote()
 265     tomboy.DisplayNote(n)
 266     tomboy.SetNoteContents(n, text)
 267 
 268 #Set Pidgin status
 269 def set_pidgin_status_text(message):
 270     bus = dbus.SessionBus()
 271     obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
 272     purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
 273 
 274     current = purple.PurpleSavedstatusGetType(purple.PurpleSavedstatusGetCurrent())
 275     status = purple.PurpleSavedstatusNew("", current)
 276     purple.PurpleSavedstatusSetMessage(status, message)
 277     purple.PurpleSavedstatusActivate(status)
 278 
 279 #How to embed a WebKit renderer in an application:
 280 import gtk, webkit
 281 gtk.gdk.threads_init()
 282 
 283 window = gtk.Window()
 284 window.connect("destroy", gtk.main_quit)
 285 web = webkit.WebView()
 286 web.open("http://ubuntu.com")
 287 
 288 window.add(web)
 289 window.show_all()
 290 gtk.main()
 291 
 292 # Put an icon in a Gtk+ text entry
 293 GtkWidget *entry = gtk_entry_new();
 294 gtk_entry_set_icon_from_stock(
 295     GTK_ENTRY(entry),           // specify which entry widget
 296     GTK_ENTRY_ICON_SECONDARY,   // put the icon at the end
 297     GTK_STOCK_ABOUT);           // make it display the "about" icon
 298 
 299 # Display a progress bar in a Gtk+ entry
 300 GtkWidget *entry = gtk_entry_new();
 301 gtk_entry_set_text(GTK_ENTRY(entry), "This is a test");
 302 gtk_entry_set_progress_fraction(GTK_ENTRY(entry), 0.5);
 303 
 304 #Get Gtk+ theme colors
 305 def get_theme_colors(w):
 306     d = {}
 307 
 308     for i in ["base", "text", "fg", "bg"]:
 309         d[i] = Color.from_gtk_color(
 310             getattr(w.get_style(), i)[gtk.STATE_NORMAL].to_string())
 311 
 312         d["%s_selected" % i] = Color.from_gtk_color(
 313             getattr(w.get_style(), i)[gtk.STATE_SELECTED].to_string())
 314 
 315     return d
 316 
 317 # Draw a rounded rectangle in Cairo
 318 c = self.window.cairo_create()
 319 x,y,w,h = w.allocation # x, y, width, height
 320 
 321 c.move_to(x+r,y)
 322 c.line_to(x+w-r,y);   c.curve_to(x+w,y,x+w,y,x+w,y+r)
 323 c.line_to(x+w,y+h-r); c.curve_to(x+w,y+h,x+w,y+h,x+w-r,y+h)
 324 c.line_to(x+r,y+h);   c.curve_to(x,y+h,x,y+h,x,y+h-r)
 325 c.line_to(x,y+r);     c.curve_to(x,y,x,y,x+r,y)
 326 c.close_path()
 327 c.fill()
 328 
 329 # A Gtk+ label that supports text wrapping with fluid reflow
 330 
 331 import gtk, pango, gobject
 332 
 333 class WrapLabel(gtk.Frame):
 334     def __init__(self, markup = None, text = None):
 335         gtk.Frame.__init__(self)
 336         self.set_shadow_type(gtk.SHADOW_NONE)
 337 
 338         self.pango_layout = self.create_pango_layout(text or "")
 339         if markup: self.pango_layout.set_markup(markup)
 340         self.pango_layout.set_wrap(pango.WRAP_WORD_CHAR)
 341 
 342         ev = gtk.EventBox()
 343         ev.set_visible_window(False)
 344         self.add(ev)
 345 
 346     def do_expose_event(self, event):
 347         gtk.Frame.do_expose_event(self, event)
 348         self.set_size_request(-1, (self.pango_layout.get_size()[1] // pango.SCALE + 10))
 349 
 350         x,y,w,h = self.allocation
 351         gc = self.window.new_gc()
 352         self.pango_layout.set_width(w * pango.SCALE)
 353         self.window.draw_layout(gc, x, y, self.pango_layout)
 354 
 355 gobject.type_register(WrapLabel)
 356 
 357 w = gtk.Window()
 358 w.connect("destroy", gtk.main_quit)
 359 
 360 label = WrapLabel("This is a test")
 361 w.add(label)
 362 
 363 w.show_all()
 364 gtk.main()
 365 
 366 # Build a Gtk+ toolbar with UIManager
 367 def on_action(action):
 368     print "Action performed!"
 369 
 370 actions = gtk.ActionGroup("Actions")
 371 actions.add_actions([
 372         ("bold", gtk.STOCK_BOLD, "_Bold", "<ctrl>B", None, on_action),
 373         ("italic", gtk.STOCK_ITALIC, "_Italic", "<ctrl>I", None, on_action),
 374         ("underline", gtk.STOCK_UNDERLINE, "_Underline", "<ctrl>U", None, on_action),
 375         ])
 376 
 377 ui_def = """
 378 <toolbar name="toolbar_format">
 379   <toolitem action="bold" />
 380   <toolitem action="italic" />
 381   <toolitem action="underline" />
 382 </toolbar>
 383 """
 384 
 385 ui = gtk.UIManager()
 386 ui.insert_action_group(actions)
 387 ui.add_ui_from_string(ui_def)
 388 
 389 vb = gtk.VBox()
 390 vb.pack_start(ui.get_widget("/toolbar_format"), False)
 391 vb.pack_start(editor, True)
 392 
 393 
 394 # what to do instead of using eval() [this is safe security-wise]
 395 import ast
 396 ast.literal_eval ('[1,2,3,(4,5,6)]')
 397 
 398 # How to use doctests well.
 399 # Don't.
 400 
 401 # getting your packages updated
 402 # import os
 403 # os.system('evolution mailto:seb128@debian.org?subject=hi')
 404 # actually, he'll notice by himself.
 405 from time import sleep
 406 sleep (8 * 60 * 60) # go to bed
 407 
 408 # How to set up a threaded GTK+ app
 409 import pygtk
 410 pygtk.require('2.0')
 411 import gobject
 412 import gtk
 413 
 414 from threading import Thread
 415 
 416 
 417 class Window(gtk.Window):
 418     '''GtkWindow wrapper'''
 419 
 420     def __init__(self):
 421         gtk.Window.__init__(self)
 422         self.set_title("Threaded GTK+ Sample")
 423         self.set_default_size(320, 240)
 424         Thread(target=self.timeout_thread).start()
 425 
 426     def timeout_thread():
 427         '''Threaded timeout demo'''
 428         gtk.gdk.threads_enter()
 429         self.show()
 430         gtk.gdk_threads_leave()
 431 
 432 if __name__ == "__main__":
 433     gobject.threads_init()
 434     gtk.gdk.threads_init()
 435     try:
 436         win = Window()
 437         win.show()
 438         gtk.gdk.threads_enter()
 439         gtk.main()
 440         gtk.gdk.threads_leave()
 441     except KeyboardInterrupt:
 442         gtk.main_quit()
 443 
 444 
 445 
 446 #set the text on a pygtk widget in a Quickly app
 447 widge = self.builder.get_object("object_name").set_text(my_string)
 448 
 449 #get the text for a pygtk widdget in a Quickly app
 450 self.builder.get_object("object_name").get_text(my_string)
 451 
 452 #download an image from the web and save it
 453 import urllib
 454 img_stream = urllib.urlopen(url)
 455 img_file = open(fname,'w')
 456 img_file.write(img_stream.read())
 457 del img_file
 458 
 459 
 460 #download a large file using a read buffer, saves memory
 461 import urllib
 462 img_stream = urllib.urlopen(url)
 463 img_file = open(fname,'w')
 464 while 1:
 465     buf = img_stream.read(4096)
 466     if buf == "":
 467         break
 468     img_file.write(buf)
 469 del img_file
 470 
 471 # Still dealing with large files
 472 # Compute the md5sum of a file without loading it all in memory
 473 f = open(filepath)
 474 digest = md5.new()
 475 while 1:
 476     buf = f.read(4096)
 477     if buf == "":
 478         break
 479     digest.update(buf)
 480 f.close()
 481 print digest.hexdigest()
 482 
 483 
 484 
 485 #how to use couchgrid and change the label of the column
 486 from desktopcouch.records.couchgrid import CouchGrid
 487 db="db name"
 488 record_type="record type"
 489 #the keys field is for to label the column and give a discription of what is in that column
 490 keys=["key","another key"]
 491 self.couchgrid=CouchGrid(db,record_type=record_type,keys=keys)
 492 self.couchgrid.show()
 493 
 494 #put the couchgrid into a vbox
 495 self.builder.get_object("vbox1").pack_end(self.couchgrid)
 496 self.couchgrid.editable=True
 497 
 498 #change the name of the couchgrid column
 499 couchgrid.get_column(0).set_title("some name")
 500 
 501 #make the couchgrid column name dissapear
 502 self.couchgrid.set_headers_visible (False)
 503 
 504 #how to get stuff from a desktop couch db
 505 
 506 from desktopcouch.records.server import CouchDatabase
 507 from desktopcouch.records.record import Record
 508 
 509 __couchdb = CouchDatabase("db name",create=True)
 510 record_type="record type"
 511 results = __couchdb.get_records(record_type=record_type,create_view=True)
 512 
 513 #this takes the output of results and puts it into a string x
 514 x=""
 515 for r in results:
 516     x+=r.value["key"]+" "
 517 
 518 # PDB tip
 519 pp locals()
 520 
 521 
 522 # Parsing a line-based text file with separators
 523 # Not that advanced, but I seem to have to write this all the time.
 524 
 525 def parse_line(lines, separator):
 526     for line in lines:
 527         yield [token.strip() for token in line.split(separator)]
 528 
 529 #create a pygtk window on the fly
 530 test_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 531 test_window.connect("destroy", gtk.main_quit)
 532 test_window.set_title("my title")
 533 test_window.set_default_size(200,75)
 534 test_window.show()
 535 
 536 
 537 # reading a bunch of stuff out of a function
 538 total = []
 539 for chunk in iter(lambda: file.read(4096), ''):
 540     total.append(chunk)
 541 result = ''.join(total)
 542 
 543 
 544 # Transposing a list of lists.
 545 zip(*list_of_lists)
 546 
 547 
 548 # easy (if not efficient) sorted merging of two lists (works also for dicts)
 549 l1 = [4, 5, 6]
 550 l2 = [4, 6, 7]
 551 for item in sorted(set(l1).union(l2)):
 552     do_a_thing(item)
 553 
 554 
 555 #add a message indicator
 556 import indicate, time
 557 
 558 def display(indicator):
 559     print "Ah, my indicator has been displayed"
 560 
 561 def server_display(server):
 562     print "Ah, my server has been displayed"
 563 
 564 server = indicate.indicate_server_ref_default()
 565 server.set_type("message.im")
 566 server.set_desktop_file("/usr/share/applications/empathy.desktop")
 567 server.connect("server-display", server_display)
 568 indicator = indicate.Indicator()
 569 indicator.set_property("subtype", "im")
 570 indicator.set_property("sender", "IM Client Test")
 571 indicator.set_property_time("time", time.time())
 572 indicator.show()
 573 indicator.connect("user-display", display)
 574 
 575 
 576 # post a microblog message using gwibber from karmic
 577 bus = dbus.SessionBus()
 578 db_mb_obj = bus.get_object("com.Gwibber", "/com/gwibber/Microblog")
 579 microblog = dbus.Interface(db_mb_obj, "com.Gwibber")
 580 import gwibber, gwibber.config
 581 
 582 accounts = gwibber.config.Accounts()
 583 args = ["This is a test message"]
 584 for account in accounts:
 585     if account["send_enabled"]:
 586         acctid = account["id"]
 587     microblog.operation({
 588         "source": "lolz",
 589         "id": "send-%s-%s" % (acctid, time.time()),
 590         "accountid": acctid,
 591         "args": args,
 592         "opname": "send",
 593         })
 594 
 595 
 596 # post a microblog message using gwibber in lucid (not yet uploaded)
 597 import gwibber.utils
 598 foo = gwibber.utils.GwibberPublic()
 599 foo.post("This is a test message")
 600 
 601 
 602 
 603 
 604 # parse command line arguments
 605 import optparse
 606 op = optparse.OptionParser('%prog [options] inputfile')
 607 op.add_option('-f', '--force', action='store_true', dest='force', default=False, help='Delete my files without asking')
 608 op.add_option('-o', '--output-file', dest='outfile', help='Output file path; if not given, output to stdout')
 609 (opts, args) = op.parse_args()
 610 if opts.force:
 611     print 'force enabled'
 612 input_file = args[0]
 613 if opts.outfile:
 614     outfile = open(opts.outfile, 'w')
 615 else:
 616     outfile = sys.stdout
 617 
 618 
 619 # SQLite
 620 import sqlite3 as dbapi2
 621 db = dbapi2.connect('mydb.sqlite')
 622 
 623 cur1 = db.cursor()
 624 cur1.execute('CREATE TABLE friends (name VARCHAR(255), phone int)')
 625 cur1.commit()
 626 
 627 cur1 = db.cursor()
 628 cur1.execute('SELECT phone FROM friends WHERE name = ?', ['Fred']) # does proper quoting
 629 for (phone) in cur1:
 630     print 'Fred has telephone number', phone
 631 
 632 
 633 # Apply file rights from src to dest
 634 import os, stat
 635 st = os.stat(src_file_name)
 636 mode = stat.S_IMODE(st.st_mode)
 637 os.chmod(dest_file_name, mode)
 638 
 639 
 640 
 641 #translations using gettext
 642 import gettext
 643 
 644 #print hello world(it needs to be translated first though)
 645 print gettext.gettext("hello")
 646 
 647 
 648 # A really simple unittest module.
 649 
 650 import unittest
 651 
 652 class TestThing(unittest.TestCase):
 653 
 654     def setUp(self):
 655         super(unittest.TestCase, self).setUp()
 656         self.data = [2, 3, 5]
 657 
 658     def test_list_reversal(self):
 659         backwards = reversed(self.data)
 660         self.assertEqual([5, 3, 2], backwards)
 661 
 662     def tearDown(self):
 663         del self.data
 664 
 665 # Note that you wouldn't really use setUp and tearDown for something this
 666 # simple. Use them when you have something fairly complicated to set up.
 667 
 668 
 669 
 670 # Look for a program that is already registered over dbus
 671 import dbus
 672 session_bus = dbus.SessionBus()
 673 if session_bus.request_name ('net.soft.name') != 1:
 674     print 'someone already has this name'
 675     exit()
 676 
 677 #open a file from a dialog
 678 dialog = gtk.FileChooserDialog("Select a file", w, gtk.FILE_CHOOSER_ACTION_OPEN,
 679     (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
 680 
 681 if dialog.run() == gtk.RESPONSE_OK:
 682     fn = dialog.get_filename()
 683     if os.path.exists(fn):
 684         print fn
 685 dialog.destroy()