Difference between revisions of "Python/scripts"

From Christoph's Personal Wiki
Jump to: navigation, search
(New page: This article will list a bunch of quick-and-dirty Python tips-and-tricks, examples, code, etc. ==Lambda functions== In stead of creating a new function of f(x): return x + 1 You coul...)
 
Line 1: Line 1:
 
This article will list a bunch of quick-and-dirty [[Python]] tips-and-tricks, examples, code, etc.
 
This article will list a bunch of quick-and-dirty [[Python]] tips-and-tricks, examples, code, etc.
 +
 +
==Generate a random set of characters==
 +
''Note: Useful for creating random user passwords after a reset.''
 +
$ python -c 'import random; print "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789\!@#$%^&*(-_=+)") for i in range(9)])'
  
 
==Lambda functions==
 
==Lambda functions==

Revision as of 10:17, 13 April 2012

This article will list a bunch of quick-and-dirty Python tips-and-tricks, examples, code, etc.

Generate a random set of characters

Note: Useful for creating random user passwords after a reset.

$ python -c 'import random; print "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789\!@#$%^&*(-_=+)") for i in range(9)])'

Lambda functions

In stead of creating a new function of

f(x): return x + 1

You could use a lambda function inline, like so:

function_var = lambda x: x + 1
>>> print function_var(2)
3