Skip to main content

Home/ Groups/ Mac Attack
Benjamin Bandt-Horn

The Best Movies for Programmers - 0 views

  •  
    The Best Movies for ProgrammersIf you've done even a little programming then you're also good at finding ways to waste time watching things vaguely related to it.  Admit it, you've spent at least a few hours watching videos about Android's latest features or Chrome's Internals even though you'll never write a single line of code for either.  Don't worry, I'm not going to list a bunch of technical talks.  We're talking entertainment for after the work day.  The following movies are stimulating to watch for those with a programming oriented mind.  And you know what that means, softcore porn!
Benjamin Bandt-Horn

Ternary conditional operator in Python - Stack Overflow - 0 views

  •  
    Yes, it was added in version 2.5. It's frowned upon by some pythonistas, so keep that in mind. The syntax is: a if test else b First test is evaluated, then either a or b is returned based on the Boolean value of test; if test evaluates to True a is returned, else b is returned. For example: >>> 'true' if True else 'false' 'true' >>> 'true' if False else 'false' 'false'
Benjamin Bandt-Horn

BitwiseOperators - Python Wiki - 0 views

  • x << yReturns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.
  • x >> yReturns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.
  • x & yDoes a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.
  • ...5 more annotations...
  • x ^ yDoes a "bitwise exclusive or". Each bit of the output is the same as the corresponding bit in x if that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1.
  • x | yDoes a "bitwise or". Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1.
  • ~ x Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1.
  • Just remember about that infinite series of 1 bits in a negative number, and these should all make sense.
  • One more point: Python allows operator overloading, so some classes may be written to allow the bitwise operators, but with some other meaning. For instance, the new sets module for Python 2.3 uses | and & for union and intersection.
  •  
    The Operators: x << y Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.
Benjamin Bandt-Horn

Map. - 0 views

  •  
    Combining these two special cases, we see that map(None, list1, list2) is a convenient way of turning a pair of lists into a list of pairs. For example:         >>> seq = range(8)        >>> map(None, seq, map(lambda x: x*x, seq))        [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]        >>>
Benjamin Bandt-Horn

Python : map and filter vs for loops - 0 views

  • Re: Python : map and filter vs for loops The underlying implementation of map and filter are different than loops, even though they accomplish the same thing. Which idiom runs fastest can change from version to version. Right now I think list comprehensions are the fastest. So instead of Code: filter(f, range(2,25)), the list comprehension would be Code: [x for x in range(2,25) if f(x)] and instead of Code: map(cube, range(1, 11)) the list comprehension would be Code: [cube(x) for x in range(1,11)]
  •  
    Python : map and filter vs for loops The underlying implementation of map and filter are different than loops, even though they accomplish the same thing. Which idiom runs fastest can change from version to version. Right now I think list comprehensions are the fastest.
Benjamin Bandt-Horn

!performance - Python string formatting: % vs. .format - Stack Overflow - 0 views

  • which is just ugly. .format doesn't have those issues. Also in the second example you gave, the .format example is much cleaner looking.
  •  
    .format just seems more sophisticated in many ways. You can do stuff like re-use arguments, which you can't do with %. An annoying thing about % is also how it can either take a variable or a tuple
Benjamin Bandt-Horn

Unicode and new style string formatting ~ The Voidspace Techie Blog - 0 views

  • Unicode and new style string formatting Python 2.6 and Python 3 gain a new style of string formatting, which is apparently based on the string formatting in C#. I wasn't a big fan of the string formatting in C# and so wasn't very excited about it moving into Python, but as is to be expected it has grown a bit on me.
  • As always, the best solution is to not mix Unicode and byte-strings but to keep all strings in Unicode and only perform the encode when actually needed.
  • So why does this behaviour matter? Well it particularly matters for framework authors formatting messages based on 'user' input. This is the case with unittest, which creates error messages when tests fail. The error messages internally in unittest are byte-strings and they are often mixed with user supplied messages using string formatting. We use old-style (% based) formatting, so if the user supplies byte-strings then the resulting messages will be byte-strings. If the user supplies Unicode strings then the resulting messages will be in Unicode. Because all the internal unittest messages are ascii only we can guarantee than an implicit decode to Unicode will succeed - so the user can choose the output type by varying the type of the messages they provide.
  •  
     Python 2.6 and Python 3 gain a new style of string formatting, which is apparently based on the string formatting in C#. I wasn't a big fan of the string formatting in C# and so wasn't very excited about it moving into Python, but as is to be expected it has grown a bit on me.
Benjamin Bandt-Horn

''.format_map() in Python 2.x - 0 views

  • """''.format_map() in Python 2.x"""
  •  
    ''.format_map() in Python 2.x
Benjamin Bandt-Horn

Stack data structure in python - Stack Overflow - 0 views

  • No need to jump through these loops, See 5.1.1 Using Lists as Stacks
Benjamin Bandt-Horn

The new print function in Python 3.x - Stack Overflow - 0 views

  • If print() is a function, it would be much easier to replace it within one module (just def print(*args):...) or even throughout a program (e.g. by putting a different function in __builtin__.print). As it is, one can do this by writing a class with a write() method and assigning that to sys.stdout -- that's not bad, but definitely a much larger conceptual leap, and it works at a different level than print.
Benjamin Bandt-Horn

python - *args and **kwargs? - Stack Overflow - 0 views

  •  
    One case where *args and **kwargs are useful is when writing wrapper functions (such as decorators) that need to be able accept arbitrary arguments to pass through to the function being wrapped. For example, a simple decorator that prints the arguments and return value of the function being wrapped: def mydecorator( f ):   @functools.wraps( f )      def wrapper( *args, **kwargs ): ...
Benjamin Bandt-Horn

3. An Informal Introduction to Python - Python v2.7.6 documentation - 0 views

  •  
    Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. List items need not all have the same type.
« First ‹ Previous 61 - 73 of 73
Showing 20 items per page