#require pygments serve $ cat <> $HGRCPATH > [extensions] > highlight = > [web] > pygments_style = friendly > EOF $ hg init test $ cd test create random Python file to exercise Pygments $ cat < primes.py > #!/usr/bin/env python > > """Fun with generators. Corresponding Haskell implementation: > > primes = 2 : sieve [3, 5..] > where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0] > """ > > from itertools import dropwhile, ifilter, islice, count, chain > > def primes(): > """Generate all primes.""" > def sieve(ns): > p = ns.next() > # It is important to yield *here* in order to stop the > # infinite recursion. > yield p > ns = ifilter(lambda n: n % p != 0, ns) > for n in sieve(ns): > yield n > > odds = ifilter(lambda i: i % 2 == 1, count()) > return chain([2], sieve(dropwhile(lambda n: n < 3, odds))) > > if __name__ == "__main__": > import sys > try: > n = int(sys.argv[1]) > except (ValueError, IndexError): > n = 10 > p = primes() > print "The first %d primes: %s" % (n, list(islice(p, n))) > EOF $ hg ci -Ama adding primes.py hg serve $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log $ cat hg.pid >> $DAEMON_PIDS hgweb filerevision, html $ ("$TESTDIR/get-with-headers.py" localhost:$HGPORT 'file/tip/primes.py') \ > | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mf\"/class=\"mi\"/g" 200 Script output follows test: 853dcd4de2a6 primes.py

view primes.py @ 0:853dcd4de2a6

a
author test
date Thu, 01 Jan 1970 00:00:00 +0000
parents
children
line wrap: on
line source
  #!/usr/bin/env python
  
  """Fun with generators. Corresponding Haskell implementation:
  
  primes = 2 : sieve [3, 5..]
      where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0]
  """
  
  from itertools import dropwhile, ifilter, islice, count, chain
  
  def primes():
      """Generate all primes."""
      def sieve(ns):
          p = ns.next()
          # It is important to yield *here* in order to stop the
          # infinite recursion.
          yield p
          ns = ifilter(lambda n: n % p != 0, ns)
          for n in sieve(ns):
              yield n
  
      odds = ifilter(lambda i: i % 2 == 1, count())
      return chain([2], sieve(dropwhile(lambda n: n < 3, odds)))
  
  if __name__ == "__main__":
      import sys
      try:
          n = int(sys.argv[1])
      except (ValueError, IndexError):
          n = 10
      p = primes()
      print "The first %d primes: %s" % (n, list(islice(p, n)))
hgweb fileannotate, html $ ("$TESTDIR/get-with-headers.py" localhost:$HGPORT 'annotate/tip/primes.py') \ > | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mi\"/class=\"mf\"/g" 200 Script output follows test: primes.py annotate

annotate primes.py @ 0:853dcd4de2a6

a
author test
date Thu, 01 Jan 1970 00:00:00 +0000
parents
children
rev   line source
test@0 1 #!/usr/bin/env python
test@0 2
test@0 3 """Fun with generators. Corresponding Haskell implementation:
test@0 4
test@0 5 primes = 2 : sieve [3, 5..]
test@0 6 where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0]
test@0 7 """
test@0 8
test@0 9 from itertools import dropwhile, ifilter, islice, count, chain
test@0 10
test@0 11 def primes():
test@0 12 """Generate all primes."""
test@0 13 def sieve(ns):
test@0 14 p = ns.next()
test@0 15 # It is important to yield *here* in order to stop the
test@0 16 # infinite recursion.
test@0 17 yield p
test@0 18 ns = ifilter(lambda n: n % p != 0, ns)
test@0 19 for n in sieve(ns):
test@0 20 yield n
test@0 21
test@0 22 odds = ifilter(lambda i: i % 2 == 1, count())
test@0 23 return chain([2], sieve(dropwhile(lambda n: n < 3, odds)))
test@0 24
test@0 25 if __name__ == "__main__":
test@0 26 import sys
test@0 27 try:
test@0 28 n = int(sys.argv[1])
test@0 29 except (ValueError, IndexError):
test@0 30 n = 10
test@0 31 p = primes()
test@0 32 print "The first %d primes: %s" % (n, list(islice(p, n)))
hgweb fileannotate, raw $ ("$TESTDIR/get-with-headers.py" localhost:$HGPORT 'annotate/tip/primes.py?style=raw') \ > | sed "s/test@//" > a $ echo "200 Script output follows" > b $ echo "" >> b $ echo "" >> b $ hg annotate "primes.py" >> b $ echo "" >> b $ echo "" >> b $ echo "" >> b $ echo "" >> b $ cmp b a || diff -u b a hgweb filerevision, raw $ ("$TESTDIR/get-with-headers.py" localhost:$HGPORT 'file/tip/primes.py?style=raw') \ > > a $ echo "200 Script output follows" > b $ echo "" >> b $ hg cat primes.py >> b $ cmp b a || diff -u b a hgweb highlightcss friendly $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT 'highlightcss' > out $ head -n 4 out 200 Script output follows /* pygments_style = friendly */ $ rm out errors encountered $ cat errors.log $ "$TESTDIR/killdaemons.py" $DAEMON_PIDS Change the pygments style $ cat > .hg/hgrc < [web] > pygments_style = fruity > EOF hg serve again $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log $ cat hg.pid >> $DAEMON_PIDS hgweb highlightcss fruity $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT 'highlightcss' > out $ head -n 4 out 200 Script output follows /* pygments_style = fruity */ $ rm out errors encountered $ cat errors.log $ cd .. $ hg init eucjp $ cd eucjp $ python -c 'print("\265\376")' >> eucjp.txt # Japanese kanji "Kyo" $ hg ci -Ama adding eucjp.txt $ hgserveget () { > "$TESTDIR/killdaemons.py" $DAEMON_PIDS > echo % HGENCODING="$1" hg serve > HGENCODING="$1" hg serve -p $HGPORT -d -n test --pid-file=hg.pid -E errors.log > cat hg.pid >> $DAEMON_PIDS > > echo % hgweb filerevision, html > "$TESTDIR/get-with-headers.py" localhost:$HGPORT "file/tip/$2" \ > | grep '
' > echo % errors encountered > cat errors.log > } $ hgserveget euc-jp eucjp.txt % HGENCODING=euc-jp hg serve % hgweb filerevision, html % errors encountered $ hgserveget utf-8 eucjp.txt % HGENCODING=utf-8 hg serve % hgweb filerevision, html % errors encountered $ hgserveget us-ascii eucjp.txt % HGENCODING=us-ascii hg serve % hgweb filerevision, html % errors encountered $ cd ..