Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, 30 July 2008

Python conditional expressions

Available from 2.5.

>>> word = "banana" if 0 is 0 else "peel"
>>> word
'banana'

>>> word = "banana" if 1 is 0 else "peel"
>>> word
'peel'

Tuesday, 29 July 2008

python list to string

So easy I always forget...

>>> l = ['1', '2', '3']
>>> ' '.join(l)
'1 2 3'

works only on lists of strings though.

>>> l = [1, 2, 3]
>>> ' '.join(l)
Traceback (most recent call last):
File "", line 1, in ?
TypeError: sequence item 0: expected string, int found

Wednesday, 28 May 2008

Executing python scripts through nuke-python

Python scripts can be executed through nuke's python interpreter using the following method.

cat [py script] | nuke -t

A fudge to be sure, but it does work.

Sunday, 25 May 2008

urllib

This has interesting implications used with shotreport.


import urllib
def downloadUrl(url):

data = urllib.urlopen(url)
return data.readlines()

url = 'http://www.pycode.com'
print downloadUrl(url)

Friday, 23 May 2008

Comprehensions - List and Dict

List Comprehensions

>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8]
>>> [i for i in numbers]
[1, 2, 3, 4, 5, 6, 7, 8]
>>> [i for i in numbers if i % 2 == 0]
[2, 4, 6, 8]

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f']
>>> [i for i in letters]
['a', 'b', 'c', 'd', 'e', 'f']
>>> [i for i in letters if i < 'c'] ['a', 'b'] >>> words = ['cow', 'banana', 'boat', 'moat', 'float', 'wrote']
>>> [i for i in words]
['cow', 'banana', 'boat', 'moat', 'float', 'wrote']
>>> [i for i in words if i.count('oat')]
['boat', 'moat', 'float']

>>> sentence = 'The quick brown fox jumped over the lazy cow!'
>>> [word for word in sentence.split() if word.count('a') or word.count('x')]
['fox', 'lazy']

Generator Expressions
>>> stuff = [1, 2, 3, 'cow']
>>> res = (x for x in stuff)
>>> type(res)

Dictionary Comprehension's
>>> tmp = ['a','b','c']
>>> d = dict((x, x*2) for x in tmp)
>>> d
{'a': 'aa', 'c': 'cc', 'b': 'bb'}

>>> d = dict((x, x*2) for x in range(5))
>>> d
{0: 0, 1: 2, 2: 4, 3: 6, 4: 8}

>>> tmp = ['a','b','c']
>>> d = dict((x[0], x[1]*2) for x in enumerate(tmp))
>>> d
{0: 'aa', 1: 'bb', 2: 'cc'}




Saturday, 15 March 2008

Emacs + Python: cheatsheet

Some Useful Emacs + Python Mode commands.

Starting Emacs
  • emacs --debug-init
Navigation
  • C-M-a: Go to the beginning of the current function or class - This doesn't always work for me
  • C-M-e: Go to the end of the current function or class
  • C-c C-n: Jump to the next statement.
  • C-c C-u: Go up one block
  • C-l : Centre screen on cursor.
  • C-f Move forward a character
  • C-b Move backward a character
  • M-f Move forward a word
  • M-b Move backward a word
  • C-n Move to next line
  • C-p Move to previous line
  • C-a Move to beginning of line
  • C-e Move to end of line
  • M-a Move back to beginning of sentence
  • M-e Move forward to end of sentence
  • C-v move forward one screen
  • M-v move backward one screen
  • M-<> go to start/end of file

Deleting

  • : Delete the character just before the cursor
  • C-d : Delete the next character after the cursor
  • M- :Kill the word immediately before the cursor
  • M-d : Kill the next word after the cursor
  • C-k : Kill from the cursor position to end of line
  • M-k : Kill to the end of the current sentence
  • M-\ Delete spaces and tabs around point (delete-horizontal-space).
  • M-SPC Delete spaces and tabs around point, leaving one space (just-one-space).
  • C-x C-o Delete blank lines around the current line (delete-blank-lines).\
  • M-^Join two lines by deleting the intervening newline, along with any indentation following it (delete-indentation).Kill Commands
  • C-w :Kill region (from point to the mark) (kill-region).
  • M-d :Kill word (kill-word). See section Words.
  • M-DEL :Kill word backwards (backward-kill-word).
  • C-x DEL :Kill back to beginning of sentence (backward-kill-sentence). See section Sentences.
  • M-k :Kill to end of sentence (kill-sentence).
  • C-M-k :Kill sexp (kill-sexp). See section Lists and Sexps.
  • M-z char :Kill through the next occurrence of char (zap-to-char).
Yanking
  • C-y:Yank last killed text (yank).
  • M-y:Replace text just yanked with an earlier batch of killed text (yank-pop).
  • M-w:Save region as last killed text without actually killing it (kill-ring-save).
  • C-M-w:Append next kill to last batch of killed text (append-next-kill).
Undo
  • C-x u or C-_
Marking
  • C-M-h: Mark the current function or class for copying, etc.
  • C-c C-k: Mark a block of text. Using this at the head of a class or function definition will mark the entire block.
Indenting
  • C-j: Insert a new line with the same indentation level as the current line
  • RET: Insert a new line with the same indentation level as the current line
  • C-c :: Check the indentation off-set
  • C-c <: Shift the region to the left
  • C-c >: Shift the region to the right
  • C-c TAB: Indent a highlighted (or marked) region
SVN

Execute
  • C-M-x: Execute the current function or class
  • C-c C-c: Execute the buffer (i.e., the file being displayed)
  • C-c C-s: Execute a Python command.
  • C-c !: Open the interactive shell
  • C-c C-w: Run Pychecker
Documentation
  • C-c ?: Show Python mode documentatio

Searching
  • C-M-s (isearch-forward-regexp).
Files
  • C-x C-f - Find file
  • C-x C-s - Save file
  • C-x C-b - List buffers
Macros
  • Start the macro with C-X (, finish the macro with C-X ).
  • Execute the macro with C-X e.
Misc
  • C-u : Repeat. Example C-u 8 C-f, move forward 8 characers. C-u 8 * inserts ********.
  • M-! : Execute shell command.
  • M-; : Comment/Uncomment region
  • C-h v data-directory - Where does emacs live.

Tuesday, 12 February 2008

Lists

Data Structures docs
Sequence Type docs


l = ['test', 'banana', 1, 'x']

l.count('banana')

Dict

Dict Creation
>>> d = {

... 1 : "test1",
... 2 : "test2",
... 3 : "test3",
... }
>>> d
{1: 'test1', 2: 'test2', 3: 'test3'}
>>>

iterkeys()
>>> for k in d.iterkeys():
... print k
...
1
2
3

itervalues()
>>> for v in d.itervalues():
... print v
...
test1
test2
test3

iteritems()
>>> for i in d.iteritems():
... print i # A tuple
... print i[0] # The key
... print i[1] # The value
...
(1, 'test1')
1
test1

iteritems() with k, v
>>> for k,v in d.iteritems():
... print k, v
...
1 test1
2 test2
3 test3

has_key(x)
>>> if d.has_key(1):
... print d[1]
...
test1

in
>>> "a" in {"a" : 1, "b" : 2}
True

# For use in python2.5+
class Foo(dict):
__missing__(self, key):
return "banana(missing)"