AST class in Python has nice function literal_eval.  literal_eval safely evaluate an encoded string containing a Python expression. The string provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. import ast >>> ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}") {'muffin': 'lolz', 'foo': 'kitty'}
Published ago by ochirkov

There is useful __getattr__ built-in func to handle missing arguments. Dive into example: class foo: def __init__(self): self.a = "a" def __getattr__(self, attribute): return "You asked for %s, but I'm giving you default" % attribute >>> bar = foo() >>> bar.a 'a' >>> bar.b "You asked for b, but I'm giving you default" >>> getattr(bar, "a") 'a' >>> getattr(bar, "b") "You asked for b, but I'm giving you default"
Published ago by ochirkov

With type func you can check data type of variable: a = 10 b = [1,2,3] type(a) == int True type(b) == list True type(a) == float False   Same effect you can get with isinstance func: isinstance(a,int) True isinstance(b,list) True isinstance(b,tuple) False c = (4,5,6) isinstance(c,tuple) True   But, isinstance also give you possibility to check data type of your var from tuple with datatypes: isinstance(a,(float, int, str)) True isinstance(a,(list, tuple, dict)) False    
Published ago by ochirkov

Lets make some short examples of most interesting built-in python functions.   1. all(iterable) Return True if all elements of the iterable are true. >>> lst = range(10) >>> all( type(i) is int for i in lst ) True >>> lst = [1,1,2,[]] >>> all( i for i in lst ) False >>> lst.append('steve') all( type(i) is int for i in lst ) False   2. any(iterable) Return True if any element of the iterable is true. If the iterable is empty, return False.  >>> lst = [1, 2, 3, -7] >>> any(lst) True >>> a = 15 >>&...
Published ago by ochirkov

23. isinstance(object, classinfo) Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. Also return true ifclassinfo is a type object (new-style class) and object is an object of that type or of a (direct, indirect or virtual) subclass thereof. >>> isinstance(Foo(), Foo) True >>> lst = [] >>> isinstance(lst, list) True >>> isinstance(lst,(list, tuple)) True   24. issubclass(class, classinfo) Return true if class is a subclass (direct, indirect or virtual) of classi...
Published ago by ochirkov

42. sorted(iterable[, cmp[, key[, reverse]]]) Return a new sorted list from the items in iterable. >>> sorted([5, 2, 3, 1, 4]) [1, 2, 3, 4, 5] >>> a = [5, 2, 3, 1, 4] >>> a.sort() >>> a [1, 2, 3, 4, 5]   >>> sorted("This is a test string from Andrew".split(), key=str.lower) ['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']   >>> student_tuples = [ ('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10), ] >>> sorted(stude...
Published ago by ochirkov

In this article I'd like to describe the difference between three built-in methods: globals(), locals() and vars() (original link). Also I'd like to show some examples of their usage. So, globals() always returns the dictionary of the module namespace locals() always returns a dictionary of the current namespace vars() returns either a dictionary of the current namespace (if called with no argument) or the dictionary of the argument Let's see an example of theese functions in different namespaces. I have the module bla.py with following content: class A(): def __init__(self, id): self.id = id print "Class locals:\t%s" ...

Python
Web development
Review Linux
Administration
Provisioning CVS
Windows AWS
Continuous Integration
NoSql Logging Web Servers FileSystems Scala
accurev ansible apach2 apache api application approaches architecture archivation argparse arguments artifactory automation awk aws backup bash batch-scripting beaver block build built-in caching call captcha cartridge case certificates cgi chain client cloud cloudformation cluster cmd coding collections command-line commands compression conditionals conversion convert cookbook copy counter cron crud css cut cygwin daemon datatype date dd debpackage decorator delattr deploy deque dict distributed-file-systems django DNS domain driver dropbox dump elasticcache elasticsearch encryption exit ext4 extra-tags failover file filename filters for form format freetds functions gerrit getattribute gid git globals glusterfs hardware hook iam indexing inheritance init install job-interview jquery ldap linux list locals logging logs logstash lookup magicmethods mail main-menu metadata metalogger mongodb moosefs mount mssql multiplatform multithreading mysql netcat nginx nosql open-ssh openldap openshift os packaging parse partition path pattern patterns permissions pid pil pip pipe playbook pool post post-commit processes production provisioning proxy putty python python-mysql recursively redirection redis register replication repr restore return review rotation scala script search selenium server setattr settings setup shipper signals singleton slots snapshot socket splunk ssh ssh-key ssl storage str string style subprocess sugar super switch syntactic syntaxhighlighter systeminfo tail tar templatetags time tls tune2fs tuple ubuntu unicode unique unix unixodbc usage usecases uuid uwsgi variable vars version vi virtualenv volume web web-server windows with_items __getattr__