We've moved!
This blog is now hosted on codetrips.com (WordPress)
All past articles are available there, and new ones will (only) appear there - come visit us!
This blog was born as a means of annotating big and small discoveries in my use of Java, Ubuntu/Linux and Android that would be easy to access from anywhere, anytime. It does beat post-it's big time, and it also adds the benefit that others might, eventually, contribute with intelligent insights.
python fun_decorators.py
"""
@copyright: AlertAvert.com (c) 2013. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Decorators
==========
Code to try out Python decorators.
Original idea from:
http://www.brianholdefehr.com/decorators-and-functional-python
@author: Marco Massenzio (m.massenzio@gmail.com)
Created on [2013-01-05]
"""
class DecoratorClass(object):
def __init__(self, klass=None, **kwargs):
"""This gets called every time the @DecoratorClass annotation
is encountered.
@param klass: the type of the class being decorated, it will be
passed in if no parameters are passed in the annotation
@type klass: type
@param kwargs: a dictionary containing any of the named
parameters passed in the decorator's declaration
@type kwargs: dict
"""
# If the decorator is declared without parameters,
# the class type will be passed in
self._name = 'default'
self._klass = None
if klass:
print 'No-args decorator for: ', klass.__name__
self._klass = klass
else:
print 'Args declared: ', kwargs
self._name = kwargs.get('name')
def __call__(self, *args, **kwargs):
"""This gets invoked every time a decorated class gets created,
with the actual arguments in what would appear to look like a
constructor call: they do not have to (and in fact, won't) match
the actual formal argument list of the
constructor of the decorated class (which may not even get invoked).
If the decorator is declared with one or more arguments
(see Different class below), then the first item in ``args`` will be the
**type** of the class being decorated (which
will not have been passed in to the __init__()).
This gets invoked immediately after self#__init__() if the decorator is
declared with one or more arguments, or when the "constructor" is invoked.
"""
print 'Decorator __call__ invoked with: ', args, kwargs
if self._klass:
print 'Decorated instance: ', self._klass.__name__
return self._klass(self._name)
else:
print 'Setting up klass'
self._klass = args[0]
# Here we inject a 'class-level' static method
self._klass.get_name = self.get_name
return self._klass
def get_name(self):
return self._name
@DecoratorClass
class Decorated(object):
def __init__(self, name):
print '>>>>> I am being decorated! <<<<<'
self.name = name
def call_me(self, *args):
print 'I\'m being called: ', self.name
print 'These are my args: ', args
# Notice how here a 'type' name (Decorated) has been completely
# 'hijacked' to point to a specific
# instance of a DecoratorClass object (this provides 'closure')
print 'what is Decorated here?', Decorated
# >> what is Decorated here? <__main__.DecoratorClass object at 0x7f34b71308d0>
@DecoratorClass(name='another')
class Different(object):
def __init__(self):
# Notice how here we are using a 'static' method
# that has been injected by the decorator
self.name = Different.get_name()
def method(self, *args):
"""This is the same implementation as Decorated#call_me(),
just to show how they behave differently
"""
print 'I\'m being called: ', self.name
print 'These are my args: ', args
def __call__(self, *args, **kwargs):
return 'Different called with: ', args, kwargs
# Here, Different is whatever DecoratorClass#__call__() returned:
# this happens to be what one would expect it to be (a Different class
# type) but that's only because the code makes it so
print 'what is Different here? ', Different
# >> what is Different here?
print 'Calling Decorated class constructor'
# Note how the params being passed here have no relation with the
# constructor argument list and in fact, the name of the 'decorated'
# class is not even given here, but in the decorator
# Due to the 'magic' of decorators, DecoratorClass#__call__() is
# instead invoked here,
# on the instance that was created at declaration
deco = Decorated(123, "Hello", foo='foo', baz='baz')
# Again, now ``deco`` here happens to be what one would expect it to
# be (an instance of the Decorated class) but solely because the code
# makes it so - it could have been anything, really
deco.call_me(1, 2, 3, 1)
# >> I'm being called: default
# >> These are my args: (1, 2, 3, 1)
print 'Creating now a Different class:'
# Here the call does actually invoke the constructor (__init__())
# for the Different class, and the returned object is again what one
# would expect it to be (an instance of Different):
diff = Different()
diff.method('quart', 'naught')
# >> I'm being called: another
# >> These are my args: ('quart', 'naught')
# Obviously, as the Different#__call__() method is defined, we can call it too:
print diff(1, 2, 3, value='val')
# >> ('Different called with: ', (1, 2, 3), {'value': 'val'})
# If we now create an entirely different instance of a Different object,
# it will still have the same 'static' method injected by the decorator:
another_diff = Different()
print 'My name is:', another_diff.name
# >> My name is: another
![]() |
| trust me - the one on the right is Jeff Bezos
A guiding principle for Amazon is its focus on "stuff that won't change over time:" for example, it's extremely unlikely that people will ever want stuff at higher prices or slower delivery.
As an entrepreneur one should equally focus on the 'basics' of the respective problem domain: ask yourself, "what are the key user requirements, that won't change in 3-5 years?"
If you want to innovate:
AWS is experiencing great growth rate in enterprise, gov't and education
Thanks to the growth of online commerce and information sharing, consumers now (rightly) feel 'entitled' to near-perfect information (eg comparison shopping).
Hence, the power is shifting from marketing to product development: businesses can no longer rely on 'less-informed' customers being bamboozled by clever advertising and marketing ploys - also, in the age of Facebook, and social media in general, word spreads quickly: this cuts both ways.
This also applies to AWS where greater transparency around resources consumption can be accessed by developers so they can optimize their code, but just as equally exposes incompetence and laziness.
At Amazon, he enables everyone on the team to 'pull the cord' - fix defects nearer the source: this is a reference to the Kaizen manufacturing principles pioneered by Toyota, where, on the assembly line, anyone is allowed (in fact, encouraged) to pull a cord, stopping the assembly line and effectively stopping production, if they see a defect that needs fixing.
This is counter-intuitive, but incredibly effective, as it fixing defects 'far from the source' (in production) is way more expensive (and may well be nigh to impossible) than during development and testing.
Operating a low-margin business is hard: there's nowhere to hide; high margins cover a lot of sins and waste (and I guess everyone of us in the audience could hear the subtitle: Oracle, HP, IBM wouldn't stand a chance, were they not able to hoodwind customers into overspending for wasteful services).
Good entrepreneurs eliminate risks, gradually, until they have a viable business: so you need a systematic way of identifying risks in your product development and distribution chain.
A question he hears often is "Can AWS go after Enterprises and startups at the same time?
Apparently the needs are very similar around low-cost, high availability of computing and storage resources.
Other areas, like certification and security, developed mostly to meet the needs of large organizations, will benefit start-ups too, at a fraction of the cost, will make them ready for the day they will no longer be "start-ups" any more, and remove the need to migrate to some more expensive, high-margin, wasteful service.
10,000 year clock
Bezos is one of the investors behind this project of having a clock that will work for 10,000 years - he made a joke about it ("yes, I know, you're all thinking 'and he seemed sane until now'"), but he undertook it as a symbol for long-term planning.
When the focus is on such a long timescale horizon, we can accomplish things that would not be otherwise feasible.
He sees it as a means to change our way of thinking to foresee and avoid dangerous activities.
Blue Origin
Another of his projects, this is the design and production of a vertical take-off reusable space vehicle: the goal is to make space travel (or, at least, sub-orbital trips) affordable; the only way to do so is to have a re-usable vehicle.
They are currently at the 3rd development iteration, and on course for sub-orbital trips.
Of course, they leveraged AWS for lots of simulations, requiring massive computing power.
|
| This was |
| This was |


log4j.logger.httpclient.wire=DEBUG
