Skip to main content

Home/ Mac Attack/ Group items tagged python

Rss Feed Group items tagged

Benjamin Bandt-Horn

python - if A vs if A is not None: - Stack Overflow - 0 views

  •  
    The statement if A: will call A.__nonzero__()
Benjamin Bandt-Horn

Great Python Tricks for avoiding unnecessary loops in your code - Udacity Forums - 0 views

  • To do element-wise operations on a list - for example, to produce a list consisting of each element of list A multiplied by 2 Method 1 [2*x for x in A] - this technique is called list comprehension Method 2 if you have defined a function double(x) which doubles x map(double, A) Method 3 define the function that doubles x on the fly, anonymously map(lambda x: 2*x, A)
  •  
    To do element-wise operations on a list - for example, to produce a list consisting of each element of list A multiplied by 2 Method 1 [2*x for x in A] - this technique is called list comprehension Method 2 if you have defined a function double(x) which doubles x map(double, A) Method 3 define the function that doubles x on the fly, anonymously map(lambda x: 2*x, A)
Benjamin Bandt-Horn

Classes & Iterators - Dive Into Python 3 - 0 views

  • Comprehensions are just a simple form of iterators. Generators are just a simple form of iterators
  • The first argument of every class method, including the __init__() method, is always a reference to the current instance of the class. By convention, this argument is named self.
  • self is not a reserved word in Python, merely a naming convention
  • ...6 more annotations...
  • To build an iterator from scratch, Fib needs to be a class, not a function.
  • in most cases), __iter__() simply returns self, since this class implements its own __next__() method.
  • a for loop will call this automatically, but you can also call it yourself manually
  • the __next__() method raises a StopIteration exception, this signals to the caller that the iteration is exhausted. Unlike most exceptions, this is not an error; it’s a normal condition that just means that the iterator has no more values to generate. If the caller is a for loop, it will notice this StopIteration exception and gracefully exit the loop. (In other words, it will swallow the exception.)
  • Do not use yield here; that’s a bit of syntactic sugar that only applies when you’re using generators. Here you’re creating your own iterator from scratch; use return instead
  • raise StopIteration
  •  
    Comprehensions are just a simple form of iterators. Generators are just a simple form of iterators
Benjamin Bandt-Horn

How to implement __iter__(self) for a container object (Python) - Stack Overflow - 0 views

  • usually __iter__() just return self if you have already define the next() method (generator object)
  • While you are looking at the collections module, consider inheriting from Sequence, Mapping or another abstract base class if that is more appropriate. Here is an example for a Sequence subclass:
  • if hasattr(self.data[0], "__iter__": return self.data[0].__iter__() return self.data.__iter__()
  •  
    if not self.data: raise StopIteration
Benjamin Bandt-Horn

object - Build a Basic Python Iterator - Stack Overflow - 0 views

  • I just found xrange() (suprised I hadn't seen it before...) and added it to the above example. xrange() is an iterable version of range() which has the advantage of not prebuilding the list
  •  
    I just found xrange() (suprised I hadn't seen it before...) and added it to the above example. xrange() is an iterable version of range() which has the advantage of not prebuilding the list
Benjamin Bandt-Horn

PyQT Tutorial - 0 views

  • a much better way to write GUI apps in Python is to use Trolltech's QT Designer to WYSIWYG-ly create a nice-looking interface, and then automatically generate the necessary code for it with pyuic (which is a UI compiler for QT that comes with the PyQT package.)
Benjamin Bandt-Horn

17.1 subprocess -- Subprocess management - 0 views

  • The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several other, older modules and functions, such as: os.system os.spawn* os.popen* popen2.* commands.*
  •  
    The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several other, older modules and functions, such as: os.system os.spawn* os.popen* popen2.* commands.*
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 - *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

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 - Difference between "if x" and "if x is not None" - Stack Overflow - 0 views

  •  
    In the following cases: test = False test = "" test = 0 test = 0.0 test = [] test = () test = {} test = set() the if test will differ: if test:#False if test is not None:#True
Benjamin Bandt-Horn

jcalderone: How to override comparison operators in Python - 0 views

  • here are the basic rules for the customization of ==, !=, <, >, <=, and >=: For all six of the above operators, if __cmp__ is defined on the left-hand argument, it is called with the right-hand argument. A result of -1 indicates the LHS is less than the RHS. A result of 0 indicates they are equal. A result of 1 indicates the LHS is greater than the RHS
  • __eq__ is not used for !=
  • For <, __lt__ is used. For >, __gt__. For <= and >=, __le__ and __ge__ respectively
  • ...1 more annotation...
  • __eq__ does an isinstance test on its argument
  •  
    here are the basic rules for the customization of ==, !=, , =: For all six of the above operators, if __cmp__ is defined on the left-hand argument, it is called with the right-hand argument. A result of -1 indicates the LHS is less than the RHS. A result of 0 indicates they are equal. A result of 1 indicates the LHS is greater than the RHS.
Benjamin Bandt-Horn

GuiceBasics - snake-guice - Basic concepts behind the Guice methodology - A simple, lig... - 0 views

  • The Dependency Injection (DI) is an essential pattern when building large systems. It forces classes to be more modular and reusable by making them depend on an interface instead of a concrete class. Simply put classes favor instances passed into the init instead of creating new instances.
  •  
    The Dependency Injection (DI) is an essential pattern when building large systems. It forces classes to be more modular and reusable by making them depend on an interface instead of a concrete class. Simply put classes favor instances passed into the init instead of creating new instances.
Benjamin Bandt-Horn

Fox toolkit - Wikipedia, the free encyclopedia - 0 views

  • The FOX toolkit is an open source, cross-platform widget toolkit, that is, a library of basic elements for building a graphical user interface (GUI). FOX stands for Free Objects for X.
  • FOX differentiates itself in the following way from other cross-platform toolkits: Tk is a cross-platform toolkit but does not have all of the widgets that FOX considers desirable. Qt had a different licensing model, which might have required a commercial license in some cases where FOX would not. (This is no longer the case starting with Qt 4.5.) wxWidgets promotes the use of native widgets on each supported platform. FLTK is a fast, low-footprint library that supports rapid application development, and requires less code to use, but lacks advanced widgets. All of these toolkits have some support for programming natively on Mac OS and/or Mac OS X platforms, which FOX currently does not support[
  •  
    The FOX toolkit is an open source, cross-platform widget toolkit, that is, a library of basic elements for building a graphical user interface (GUI). FOX stands for Free Objects for X.
Benjamin Bandt-Horn

Ocean Empire - 0 views

  •  
    OcempGUI is an abbreviation for Ocean Empire GUI and denotes a toolkit, which contains a set of various user interface elements based on and for Pygame.
Benjamin Bandt-Horn

[SLUT] - 0 views

  • ABOUT Slut is a programming framework for generative, synthetic, interactive, network-enabled graphics. Slut generates images from processes. Such processes may be simple "construction plans" or they may depend on user input. They may be drawn from incoming network data or messages that are sent to the network. Produced images may be adaptive, accumulative or static. They may look and feel like computer games or they may inform like scientific visualizations. They may be lyrical, cynical, political, intrusive (literally over the network), simply beautiful or banal.
  •  
    ABOUT Slut is a programming framework for generative, synthetic, interactive, network-enabled graphics. Slut generates images from processes. Such processes may be simple "construction plans" or they may depend on user input. They may be drawn from incoming network data or messages that are sent to the network. Produced images may be adaptive, accumulative or static. They may look and feel like computer games or they may inform like scientific visualizations. They may be lyrical, cynical, political, intrusive (literally over the network), simply beautiful or banal.
Benjamin Bandt-Horn

http://www.partiallydisassembled.net/euclid.html - 0 views

  •  
    euclid.py
Benjamin Bandt-Horn

http://inventwithpython.com/pygame/chapter2.html - 0 views

  • Setting Up a Pygame Program
  •  
    Setting Up a Pygame Program
« First ‹ Previous 41 - 60 of 60
Showing 20 items per page