class a(str): def __mul__(self, other): return self.startswith(other) or other.startswith(self) a1 = a('Hi') a2 = a('Hallo') a1*a2 (returns 1) -- Advice from Holger Krekel on comp.lang.python.
>>> original = [('L0','S2'), ('S2','S0'), ('S2','S1'), ('S1','Q2'), ('S0','Q1'), ('S2','S3')] >>> tree = {} >>> for key, value in original: tree.setdefault(key,[]).append(value) >>> tree {'S2': ['S0', 'S1', 'S3'], 'S1': ['Q2'], 'S0': ['Q1'], 'L0': ['S2']} -- Advice from Raymond Hettinger on comp.lang.python. Also, see Guido's discussion on Graphs: http://www.python.org/doc/essays/graphs.html
Perhaps you can draw a PDF file with Reportlab (http://www.reportlab.com/) and printing it. Because you are using Outlook Express, I assume you need it for Windows. An example: from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import A4 c = canvas.Canvas('c:\\tmp\\test.pdf', pagesize=A4, bottomup=0) c.setFont('Helvetica', 14) c.drawString(10, 20, 'Hello World!') c.save() If you want direct printing, without user interaction and you know the path of the Acrobat Reader, you can use this line: system('"C:\\Programme\\Adobe\\Acrobat 5.0\\Reader\\AcroRd32.exe" /p /h c: \\tmp\\test.pdf') Perhaps it's easier with the ShellExecute command, because then you don't need to know the path of AcroRd32.exe, but then you need this, if you don't have ActiveState Python installed: http://starship.python.net/crew/mhammond/win32/Downloads.html If you just want to show the user the page and let the user decide to print or close the document, this lines are sufficient: from os import startfile startfile('c:\\tmp\\test.pdf') Advice from Frank Buss on comp.lang.python
>>> p=file('LPT1:','w') >>> p.write('Test Line\n\f') >>> p.flush() # p.close() also flushes Note: \f is linefeed and critical for this to work properly. Advice from Terry J. Reedy on comp.lang.python
Simple: import os p = os.popen('lpr', 'w') p.write("Hello world!\n") p.close() Complicated / using Postscript: import os p = os.popen('lpr', 'w') p.write('%!PS\n') p.write('/Helvetica findfont 12 scalefont setfont\n') p.write('/cm { 72 mul 2.54 div } def\n') p.write('1 cm 28 cm moveto\n') p.write('(Hello world!) show\n') p.write('showpage\n') p.close() Advice from Eric Brunel on comp.lang.python
import win32ui dc = win32ui.CreateDC() dc.CreatePrinterDC() dc.SetMapMode(4) # This is UI_MM_LOENGLISH ## With this map mode, 12 points is 12*100/72 units = 16 font = win32ui.CreateFont({'name' : 'Arial', 'height' : 16}) dc.SelectObject(font) dc.StartDoc("HelloWorld") dc.StartPage() dc.TextOut(40, 40, "Hello world!") dc.EndPage() dc.EndDoc() For more information on the Windows version, see the documenattion for win32ui @ http://aspn.activestate.com//ASPN/Python/Reference/Products/ActivePython/PythonWin/modules.html and Micro$oft MFC documenation @ http://msdn.microsoft.com/library/default.asp Advice from Eric Brunel on comp.lang.python
def isprime(num): if num == 1 :return 1 prog=re.compile(r"^1?$|^(11+?)\1+$") return prog.match('1'*num) is None
def lengthOfLongestStr(list): l=len(list[0]) for count in range(1,len(list)): if len(list[count])>l: l=len(list[count]) return l OR def lengthOfLongestStr(myList): return max([len(s) for s in myList]) Advice by Matthew Cowles from comp.lang.python
OR def lengthOfLongestStr(myList): return max (map (len, myList)) Advice by Paul Rubin from comp.lang.python
Instead of doing string concatentation like this: s = s1 + ' ' + s2 + ' ' + s3 do: s = "%s %s %s" % (s1, s2, s3)
I recently wanted to match among a list of keywords, repeatedly, and want ed to help the Python `re' module a bit, speed-wise. I wrote the following helper function (I stole the idea of this from Markku Rossi's `enscript') : def build regexp(tokens): # Build an optimised regular expression able to recognise all TOKENS. tokens by first = {} empty = False for token in tokens: if token == '': empty = True else: sequence = tokens by first.get(token[0]) if sequence is None: sequence = tokens by first[token[0]] = [] sequence.append(token[1:]) if not tokens by first: return '' fragments = [re.escape(letter) + build regexp(tokens) for letter, tokens in tokens by first.iteritems()] if empty: fragments.append('') if len(fragments) == 1: return fragments[0] return '(?:%s)' % '|'.join(fragments) Given the above, build regexp(['this', 'that', 'the-other']) yields the string 'th(?:is|at|e\\-other)', which one may choose to `re.compile' before use. By Francois Pinard
import sys sys.path.append(additionaldirectory)Advice by Tim Delaney on comp.lang.python
You can also make it even more general: class MyData: def __init__(self, **args): self.__dict__.update(args) d = MyData(firstName='Max M', lastName='Rasmussen') print d.firstName, d.lastName -- Advice on comp.lang.python.