Module Import alias Import alias Import alias ImportFrom alias ImportFrom alias ImportFrom alias ImportFrom alias ImportFrom alias FunctionDef arguments args NameParam defaults body Assign NameStore Call NameLoad Call AttributeLoad NameLoad attr NameLoad keyword NameLoad keyword Num keyword TupleLoad Str Str Call NameLoad Call NameLoad keyword Str Print NameLoad decorator_list FunctionDef arguments args NameParam defaults body Print Str Assign NameStore Call AttributeLoad Str attr SubscriptLoad SubscriptLoad Call AttributeLoad NameLoad attr NameLoad Index Num Slice Num Print Call NameLoad NameLoad Call NameLoad Call NameLoad keyword Str Print Str decorator_list FunctionDef arguments args NameParam defaults body Print Str Assign NameStore Call NameLoad If CompareIsNot NameLoad NameLoad body Print Call AttributeLoad Str attr NameLoad Print Str decorator_list FunctionDef arguments args defaults vararg kwarg body FunctionDef arguments args NameParam defaults body FunctionDef arguments args defaults body Expr Call NameLoad NameLoad Expr Call AttributeLoad AttributeLoad NameLoad attr attr If Call AttributeLoad NameLoad attr Str NameLoad body Expr Call NameLoad NameLoad Expr Call AttributeLoad AttributeLoad NameLoad attr attr decorator_list Call NameLoad NameLoad Return NameLoad decorator_list If BoolOpAnd CompareEq Call NameLoad NameLoad Num Call NameLoad SubscriptLoad NameLoad Index Num body Return Call NameLoad SubscriptLoad NameLoad Index Num orelse Return NameLoad decorator_list FunctionDef arguments args defaults body Expr Str decorator_list NameLoad FunctionDef arguments args defaults body Expr Str Assign NameStore Dict Str Str Assign NameStore Str If CompareIn NameLoad NameLoad body Delete SubscriptDel NameLoad Index NameLoad If CompareIn NameLoad NameLoad body Print SubscriptLoad NameLoad Index NameLoad orelse Print Str decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str Assign NameStore Dict Str Str Str Str Print Call AttributeLoad NameLoad attr Str Str Print Call AttributeLoad NameLoad attr Str Str Print Call AttributeLoad NameLoad attr Str Str Expr Call NameLoad NameLoad decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str Assign NameStore Dict Str Str Print Call AttributeLoad NameLoad attr Str Str Print Call AttributeLoad NameLoad attr Str Str Print Call AttributeLoad NameLoad attr Str decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str Assign NameStore ListLoad Num Num Num Num Num Print SubscriptLoad NameLoad Index Num decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str Assign NameStore ListLoad Num Num Num Num Num Num Num Num Num Num Assign NameStore Num Assign NameStore Num Assign NameStore Num Expr Call NameLoad SubscriptLoad NameLoad Slice NameLoad NameLoad NameLoad Expr Call NameLoad SubscriptLoad NameLoad Slice NameLoad NameLoad UnaryOpUSub NameLoad Expr Call NameLoad SubscriptLoad NameLoad Slice Num decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str Assign NameStore ListLoad Str Str Str Str Str Assign NameStore ListLoad Num Num Num Num Num Print Call NameLoad NameLoad NameLoad Expr Call NameLoad Call NameLoad Call NameLoad NameLoad NameLoad decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str Print Str Print BoolOpAnd NameLoad NameLoad NameLoad Print BoolOpAnd NameLoad NameLoad NameLoad Print BoolOpAnd NameLoad Num CompareLt Num Num Print BoolOpAnd NameLoad CompareLt Num Num Num Print Str Print BoolOpOr NameLoad NameLoad NameLoad Print BoolOpOr Num NameLoad NameLoad Print BoolOpOr NameLoad NameLoad Num Print BoolOpOr Num NameLoad NameLoad decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args NameParam NameParam defaults ListLoad body Expr Call AttributeLoad NameLoad attr NameLoad Return NameLoad decorator_list FunctionDef arguments args NameParam NameParam defaults NameLoad body Assign NameStore BoolOpOr NameLoad ListLoad Expr Call AttributeLoad NameLoad attr NameLoad Return NameLoad decorator_list FunctionDef arguments args defaults body Expr Str Expr Call NameLoad Call NameLoad Num Expr Call NameLoad Call NameLoad Num decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str Expr Call NameLoad Call NameLoad Num Expr Call NameLoad Call NameLoad Num decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str Assign NameStore Str Assign NameStore Str Assign TupleStore NameStore NameStore TupleLoad NameLoad NameLoad Print Call AttributeLoad Str attr NameLoad NameLoad decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str Assign NameStore TupleLoad Num Num Assign NameStore Dict Str Str Num Num Expr Call NameLoad Num Num Expr Call NameLoad NameLoad Expr Call NameLoad NameLoad decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str decorator_list NameLoad FunctionDef arguments args defaults body Expr Str TryExcept body Expr Str handlers ExceptHandler body Pass TryExcept body Expr Str handlers ExceptHandler type NameLoad body Pass decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body TryExcept body TryExcept body Raise Call NameLoad handlers ExceptHandler type NameLoad body Print Str handlers ExceptHandler body Print Str Print Str Print Str Print Str decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str decorator_list NameLoad FunctionDef arguments args defaults body Expr Str TryFinally body TryExcept body Assign NameStore Num handlers ExceptHandler type NameLoad body Print Str orelse Print Str finalbody Print Str decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str TryFinally body TryExcept body Assign NameStore Num handlers ExceptHandler type NameLoad body Print Str orelse Print Str Return Num finalbody Print Str Print Return Num decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args NameParam NameParam defaults body Print Call AttributeLoad Str attr NameLoad NameLoad decorator_list FunctionDef arguments args defaults body Expr Str Assign NameStore Num Assign NameStore BinOpMult Num NameLoad Print CompareLtLtLtLt Num NameLoad Num NameLoad Num Print CompareLtGt Num NameLoad Num decorator_list Call NameLoad keyword NameLoad FunctionDef arguments args defaults body Expr Str decorator_list NameLoad FunctionDef arguments args defaults body Expr Str decorator_list NameLoad FunctionDef arguments args defaults body Expr Str decorator_list NameLoad Assign NameStore Dict Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad Str TupleLoad NameLoad NameLoad TupleLoad NameLoad NameLoad FunctionDef arguments args defaults body Assign NameStore Str Assign NameStore ListLoad Assign NameStore Call AttributeLoad NameLoad attr For NameStore Call NameLoad NameLoad body If Call NameLoad SubscriptLoad SubscriptLoad NameLoad Index NameLoad Index Num Str body Expr Call AttributeLoad NameLoad attr Call AttributeLoad Str attr AttributeLoad SubscriptLoad SubscriptLoad NameLoad Index NameLoad Index Num attr NameLoad Return Call AttributeLoad Str attr NameLoad decorator_list If CompareEq NameLoad Str body Assign NameStore Str While NameLoad body TryExcept body Print Call AttributeLoad Str attr NameLoad Assign NameStore Call AttributeLoad Call AttributeLoad AttributeLoad NameLoad attr attr attr Str If CompareEq NameLoad Str body Assign NameStore NameLoad Assign TupleStore NameStore NameStore Call AttributeLoad NameLoad attr NameLoad TupleLoad NameLoad NameLoad If CompareIs NameLoad NameLoad body Expr Call AttributeLoad NameLoad attr Num orelse If CompareIsNot NameLoad NameLoad body Expr Call NameLoad orelse Print Call AttributeLoad Str attr NameLoad Call NameLoad handlers ExceptHandler type NameLoad body Raise ExceptHandler body Expr Call AttributeLoad NameLoad attr Num
N/A N/A inspect N/A json N/A sys functools wraps pygments highlight pygments.lexers PythonLexer pygments.lexers.data JsonLexer pygments.formatters TerminalFormatter pprint N/A N/A value N/A N/A N/A formatted_value N/A highlight N/A N/A json dumps value sort_keys True indent 4 separators N/A , :  N/A JsonLexer N/A TerminalFormatter bg dark N/A formatted_value N/A print_func N/A N/A func N/A N/A N/A --- Code start --- N/A code N/A N/A join N/A N/A N/A N/A inspect getsourcelines func N/A 0 N/A 1 N/A N/A highlight code N/A PythonLexer N/A TerminalFormatter bg dark N/A --- Code end --- N/A print_func_result N/A N/A func N/A N/A N/A --- Result start --- N/A result N/A func N/A N/A result None N/A N/A N/A N/A Return value: {0} format result N/A --- Result end --- N/A slide N/A N/A N/A args kwargs N/A wrap N/A N/A f N/A N/A wrapped_f N/A N/A N/A N/A N/A N/A print_func f N/A N/A N/A N/A sys stdin readline N/A N/A N/A kwargs get executable False N/A N/A N/A print_func_result f N/A N/A N/A N/A sys stdin readline N/A N/A wraps f N/A wrapped_f N/A N/A N/A N/A N/A len args 1 N/A callable N/A args N/A 0 N/A N/A N/A wrap N/A args N/A 0 N/A N/A wrap N/A start N/A N/A N/A N/A N/A 
    Good morning!
    Welcome to Python tips, tricks and dark magic
    I'm Jordi Soucheiron and I work @ serverdensity (www.serverdensity.com),
    We make an awesome SaaS tool to monitor servers and websites.
    Feel free to interrupt and ask anything during the talk
    You can reach me through:
      - @jordixou
      - jordi@soucheiron.cat

    You can download (and execute) this presentation from here:
    https://github.com/jsoucheiron/pttdm/
     N/A slide dictionaries_1 N/A N/A N/A N/A N/A  Working with dictionaries
     N/A my_dict N/A my_key my_value N/A key my_key N/A N/A key my_dict N/A N/A N/A my_dict N/A key N/A N/A key my_dict N/A N/A N/A my_dict N/A key N/A N/A default value N/A N/A slide executable True dictionaries_2 N/A N/A N/A N/A N/A  Use pop to simplify key deletion. Just remember to add a default value
    to avoid KeyExceptions
     N/A my_dict N/A my_key my_other_key my_value my_other_value N/A N/A N/A my_dict pop my_key I don't care if it doesn't exist N/A N/A N/A my_dict pop my_key I don't care if it doesn't exist N/A N/A N/A my_dict pop my_key I don't care if it doesn't exist N/A N/A pprint my_dict N/A N/A slide executable True dictionaries_3 N/A N/A N/A N/A N/A  Dictionaries have a get method. This method let's you use a optional d
    parameter with a default value of None. If you set d, if the key doesn't
    exist you'll get d.
     N/A my_dict N/A my_key my_value N/A N/A N/A my_dict get my_key default value N/A N/A N/A my_dict get my_missing_key default value N/A N/A N/A my_dict get my_missing_key N/A N/A slide executable True lists_1 N/A N/A N/A N/A N/A  Negative indexes
     N/A my_list N/A 1 2 3 4 5 N/A N/A my_list N/A -1 N/A N/A slide executable True lists_2 N/A N/A N/A N/A N/A  List slicing
     N/A my_list N/A 1 2 3 4 5 6 7 8 9 10 N/A start 1 N/A end 8 N/A step 2 N/A N/A pprint N/A my_list N/A start end step N/A N/A pprint N/A my_list N/A end start N/A step N/A N/A pprint N/A my_list N/A -1 N/A N/A slide executable True lists_3 N/A N/A N/A N/A N/A  List zipping
     N/A my_list_1 N/A a b c d e N/A my_list_2 N/A 1 2 3 4 5 N/A N/A zip my_list_1 my_list_2 N/A N/A pprint N/A dict N/A zip my_list_1 my_list_2 N/A N/A slide executable True and_or N/A N/A N/A N/A N/A  Advanced "and" and "or" usage.

    When concatenated, "and" will give back the value
    of the first non-true operand or the last operand if all the operands
    are True.
    When concatenated, "or" will give back the last operand if all are False or
    the first True operand.
     N/A ---and--- N/A N/A True False None N/A N/A True None False N/A N/A True 1 N/A 10 20 N/A N/A True N/A 10 20 1 N/A ---or--- N/A N/A True False None N/A N/A 1 None False N/A N/A False None 0 N/A N/A 0 None False N/A N/A slide executable True my_wrong_append N/A N/A value my_list N/A N/A N/A N/A N/A N/A my_list append value N/A my_list N/A my_append N/A N/A value my_list N/A None N/A N/A my_list N/A my_list N/A N/A N/A N/A my_list append value N/A my_list N/A default_arguments_1 N/A N/A N/A N/A N/A  Default arguments are only evaluated once
    Assume we have this method (we do):

def my_wrong_append(value, my_list=[]):
    my_list.append(value)
    return my_list

    What's going to happen?
     N/A N/A pprint N/A my_wrong_append 1 N/A N/A pprint N/A my_wrong_append 2 N/A N/A slide executable True default_arguments_2 N/A N/A N/A N/A N/A  So... unless you're doing it on purpose
    We can take advantage of "or" and do this:

def my_append(value, my_list=None):
    my_list = my_list or []
    my_list.append(value)
    return my_list

    And now?
     N/A N/A pprint N/A my_append 1 N/A N/A pprint N/A my_append 2 N/A N/A slide executable True unpacking_1 N/A N/A N/A N/A N/A  Unpacking
     N/A a rules! N/A b FOSDEM N/A N/A a b N/A b a N/A N/A N/A {0} {1} format a b N/A N/A slide executable True unpacking_2 N/A N/A N/A N/A N/A  Other unpacking tricks:
    We have this helper function:
def print_point(x, y):
    print("({0}, {1})".format(x, y))
     N/A point_a N/A 3 4 N/A point_b N/A y x 4 3 N/A N/A print_point 3 4 N/A N/A print_point point_a N/A N/A print_point point_b N/A N/A slide executable True unpacking_3 N/A N/A N/A N/A N/A  The most usual place where this is used is in functions overwritten in
    child classes:

    def __init__(self, *args, **kwargs):
        super(MyClass, self).__init__(*args, **kwargs)
        ...

     N/A slide exceptions_1 N/A N/A N/A N/A N/A  Exceptions

    Disclamer: DON'T EVER DO THIS. Unless:
      - You want everyone to hate you
      - You want the FSM to kill a kitten every time this code runs
     N/A N/A N/A Some very dangerous stuff N/A N/A N/A N/A N/A N/A N/A Some very dangerous stuff N/A N/A N/A Exception N/A N/A N/A N/A slide executable True exceptions_2 N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A KeyboardInterrupt N/A N/A N/A Exception N/A N/A We won't print this N/A N/A N/A N/A We won't catch the exception with Exception. N/A KeyboardInterrupt is not a subclass of Exception N/A only non-system-exiting exceptions are. N/A But they are still exceptions and, therefore, can be captured N/A N/A slide executable True exceptions_3 N/A N/A N/A N/A N/A  Exceptions follow the following hierarchy:
    BaseException
     +-- SystemExit
     +-- KeyboardInterrupt
     +-- GeneratorExit
     +-- Exception
          +-- StopIteration
          +-- StopAsyncIteration
          +-- ArithmeticError
          ....
     N/A slide flow_control_1 N/A N/A N/A N/A N/A  Full try/except/else/finally flow
     N/A N/A N/A N/A N/A a 0 N/A N/A N/A ValueError N/A N/A The exception won't be raised N/A N/A We'll run the code in else if the exception is not raised N/A N/A and we'll always run finally (great for cleanup code) N/A N/A slide executable True flow_control_2 N/A N/A N/A N/A N/A  Weird try/except/else/finally flow considerations
     N/A N/A N/A N/A N/A a 0 N/A N/A N/A ValueError N/A N/A The exception won't be raised N/A N/A We'll run the code in else if the exception is not raised N/A 1 N/A N/A and we'll always run finally even avoiding return calls N/A N/A 2 N/A N/A slide executable True print_point N/A N/A x y N/A N/A N/A N/A N/A ({0}, {1}) format x y N/A chaining_comparisons N/A N/A N/A N/A N/A  Chaining comparisons
     N/A x 15 N/A y N/A 2 x N/A N/A 10 x 20 y 50 N/A N/A 20 x 10 N/A N/A slide executable True web_server N/A N/A N/A N/A N/A In order to start a web file server on
    the current directory, simply run:
    python2.7 -m SimpleHTTPServer 5000
    or
    python3 -m http.server 5000
     N/A slide questions N/A N/A N/A N/A N/A  That's all I have for now.
    If you have any questions I'll be happy to answer them :)

    Thanks to @itorres for the index code

    I based most of this talk from the stuff I got from here:
    http://www.siafoo.net/article/52
    https://docs.python.org/3/library/functions.html
    http://stackoverflow.com/questions/101268/hidden-features-of-python
    http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html
    https://docs.python.org/3/library/exceptions.html#exception-hierarchy
     N/A slide end N/A N/A N/A N/A N/A  Thank you all!!!
    You can download the code at https://github.com/jsoucheiron/pttdm
    and you can send any questions to @jordixou
     N/A slide N/A slides N/A start 1.1 1.2 1.3 2.1 2.2 2.3 3 4.1 4.2 5.1 5.2 5.3 6.1 6.2 6.3 7.1 7.2 8 9 10 11 exit 0 N/A start 1.1 N/A dictionaries_1 1.2 N/A dictionaries_2 1.3 N/A dictionaries_3 2.1 N/A lists_1 2.2 N/A lists_2 2.3 N/A lists_3 3 N/A and_or 4.1 N/A default_arguments_1 4.2 N/A default_arguments_2 5.1 N/A unpacking_1 5.2 N/A unpacking_2 5.3 N/A unpacking_3 6.1 N/A exceptions_1 6.2 N/A exceptions_2 6.3 N/A exceptions_3 7.1 N/A flow_control_1 7.2 N/A flow_control_2 8 N/A chaining_comparisons 9 N/A web_server 10 N/A questions 11 N/A end exit N/A exit None N/A exit None index N/A N/A N/A N/A N/A r N/A names N/A N/A keys N/A N/A slides keys N/A k N/A sorted keys N/A N/A N/A hasattr N/A N/A slides N/A k N/A 0 __name__ N/A N/A N/A N/A names append N/A N/A {} ({}) format N/A N/A N/A slides N/A k N/A 0 __name__ k N/A N/A N/A join names N/A N/A N/A __name__ __main__ N/A N/A next_slide start N/A True N/A N/A N/A N/A N/A N/A Slide({0}): format next_slide N/A user_input N/A N/A N/A N/A N/A sys stdin readline strip N/A N/A user_input N/A N/A user_input next_slide N/A N/A func next_slide N/A N/A slides get user_input N/A None next_slide N/A N/A func exit N/A N/A N/A N/A sys exit 0 N/A N/A N/A func None N/A N/A N/A func N/A N/A N/A N/A Slide '{0}' not found. Available_slides:
{1} format user_input N/A index N/A N/A N/A Exception N/A N/A N/A N/A N/A N/A N/A sys exit 0