Archives for: September 2006

09/27/06

Permalink 09:20:41 pm, Categories: Python, 43 words   English (US)

Finding truth

Let's say you have a list:

foo = [True, False, False, True]

and you want to do a simple AND across all elements. Here's a quick way:

result = reduce(lambda x, y: x and y, foo)

result would be False in the above example.

09/16/06

Permalink 10:00:32 pm, Categories: Books, Science, Health, 164 words   English (US)

Attention and intelligence

From Alan Wallace's The Attention Revolution:

[William] James also asserted that geniuses of all kinds excel in their capacity for sustained voluntary attention. Just think of the greatest musicians, mathematicians, scientists, and philosophers throughout history - all of them, it seems, have had an extraordinary capacity to focus their attention with a high degree of clarity for long periods of time. A mind settled in such a state of alert equipose is a fertile ground for the emergence of all kinds of original associations and insights. Might "genius" be a potential we all share - each of us with our own unique capacity for creativity, requiring only the power of sustained attention to unlock it? A focused mind can help bring the creative spark to the surface of consciousness. The mind constantly caught up in one distraction after another, on the other hand, may be forever removed from its creative potential. Clearly, if we were to enhance our faculty of attention, our lives would improve dramatically.

09/14/06

Permalink 06:52:38 pm, Categories: Science, 145 words   English (US)

Butterfly weddings

I just learned that it is now a fad to release butterflies at weddings. The wedding planners must of course get these from 'butterfly farms.' From an interesting article in The Epoch Times:

"It's a fact that diseases spread from farm animals to wild animals," says Glassberg, who has a Ph.D. in biology from Rice University. "There's a whole raft of diseases that only affect butterflies. When you have millions of them in one area [as on butterfly farms], you foster the spread of disease, and even more dangerously, you foster the creation of new diseases. ... Releasing [butterflies] also decreases the genetic fitness of the wild organisms," he says, citing study results published in Nature magazine.

Might want to think twice before you let loose thousands of insects at your next wedding. Stick to throwing a bridal bouquet on your fellow human beings please.

09/13/06

Permalink 09:47:21 pm, Categories: Python, 277 words   English (US)

lambda curry

Curry functions are best described as cocked functions. You can do this with lambda, and it's really useful for callbacks where you'd like to add some extra data not normally given to the callback.

Let's say you have some library function that looks like:

def Foo(command, callback):
  ...

Foo takes a command, executes it, then calls callback with the status when it's finished, i.e.:

  callback(status)

Normally you'd have your own method that would be the callback and take one status argument. However, what if you'd like more information passed to your callback than just status? Let's say you want the callback to get the command as an argument as well. That's where lambda comes in. Let's say your callback looks like:

def MyCallback(command, status):
  ...

Then you would just do:

command = 'blah'
fn = lambda status: MyCallback(command, status)
Foo(command, fn)

Now MyCallback will be called with both the command and the status. The power of lambda!

Update: This is absolutely wrong. Check out this example:

>>> def foo(n, x, y):
...   return x + y + n
...
>>> num=5
>>> f = lambda x,y: foo(num, x, y)
>>> num=10
>>> f(6, 7)
23

The lambda still refers to the num variable, not its value at lambda creation. What if I want to use the value 5 above for num and not 10? That's when a curry is absolutely needed. PEP 309 explains it. Generally you can create a curry method to do this:

def curry(fn, *cargs, **ckwargs):
  def call_fn(*fargs, **fkwargs):
    d = ckwargs.copy()
    d.update(fkwargs)
    return fn(*(cargs + fargs), **d)
  return call_fn

Then for the above example I would do:

>>> num=5
>>> f = curry(foo, num)
>>> num=10
>>> f(6, 7)
18

Ah, much better!

09/11/06

Permalink 11:11:02 pm, Categories: Movies, 230 words   English (US)

Loose Change

I came across the online movie Loose Change, which offers an 'alternative' viewpoint of 9/11. Say what you will about consipiracy theories, but this documentary poses some pressing questions. I remember watching the towers fall and thinking to myself, that looks like a controlled demolition. That's exactly one of the points this movie puts forth.

The things that I found most disturbing was the supposed coverup of information. Supposedly employees of a Sheraton hotel had surveillance video that showed what happened to the Pentagon, but this video was confiscated and the employees agreed to not say anything about it. That just seems crazy to me. Also the stock market activity with put options on American Airlines and Boeing, as well as the mortgage owner of the twin towers doing some suspicious contracts shortly before the downfall really boggles me. The film talks about the multiple explosions being heard and felt both before and after the planes hit the towers, and the transported gold from one of the towers.

Some of it is convincing, some of it crazy. It's a nicely made video though, and I'd recommend watching it and making your own conclusions. Personally, I can't believe that such a coverup would be successful. So many people would have to be in on the conspiracy. I find it hard to imagine that not one of those people would have a conscience.

09/09/06

Permalink 03:22:19 pm, Categories: Python, 153 words   English (US)

try finally with except

I many times had a need to do a try/finally in Python as well as catch exceptions. This always ended up with sub-trys:

try:
  try:
    oh()
  except Blah:
    how()
finally:
  lame()

I'm happy to find out that Python 2.5 will now allow both except and finally to be used together. Even cooler is the with statement. No more needing to write boilerplate acquire/release thread locking code, but rather simply:

lock = threading.Lock()
with lock:
    # Critical section of code
    ...

Cool, automatic acquire and release. Checkout the What's New in Python 2.5, as it also contains other neat things. There is also a video of a talk Guido gave at work about Python 3000, which I unfortunately missed :(.

Btw, I used tinyurl here because of the gayness of my webhosting provider. They are blocking any http post strings containing "/fev/" (replace fev with dev) with mod_security as if this somehow makes the site more secure.

09/03/06

Permalink 11:12:41 pm, Categories: Books, 242 words   English (US)

Mission: Intercept Terror

From George Soros' The Age of Fallibility:

What makes the war on terror a false metaphor is that it is taken literally. Terror is an abstraction. One cannot wage war on an abstraction. We have the means to destroy any target as long as we can identify it, but terrorists rarely provide an identifiable target. When we declare war, we must find a target; but the target we choose is unlikely to be the right one. We have killed more innocent civilians in Iraq than the terrorists killed on 9/11. In addition to killing, we have also humiliated and tortured many Iraqis. By creating innocent victims, we have advanced the terrorists' cause. They can now depict us as the terrorists and enlist the support of theiry countrymen, just as President Bush has enlisted ours. We find this difficult to understand because we cannot envision ourselves as terrorists. Yet, that is exactly how we appear to many Iraqis.

...

Since terrorists are invisible, they will never disappear. Since the war on terror is counterproductive, it is liable to generate more terrorists or insurgents that it can liquidate. As a result, we are facing a permanent state of war and the end of the United States as an open society. All men and women of good faith, regardless of party affiliation, must come together to reject the war on terror as a false and dangerous metaphor.

Oceania is at war with Eastasia... no, I mean Eurasia.

Permalink 09:06:27 pm, Categories: Movies, 312 words   English (US)

Half Nelson

I remember seing a movie called Fresh which was about a precocious young kid growing up in the inner-city. He excels at one game: chess, whom his dead-beat father (played by Samuel L. Jackson) has taught him. This is a movie that takes a completely different look at inner-city struggle. It's about a struggle to get out of that environment, and not fall trap to the plagues that affect the majority of the community (drugs, violence, etc). The boy plays everyone as if they were chess pieces, and the strategy is only revealed towards the end of the movie. But the strategy is a success and he frees himself from the shackles that held him down in that environment. I thought it was beautiful.

Half Nelson reminded me of that movie. It's about an inner-city teacher who is good at his work but has a crack addiction which one of the students that admires him finds out. It was very moving and sometimes very heart-breaking to watch. It really made me think about how tough it must be for inner-city children to find good role-models and avoid the pitfalls that plague those communities. It really is a struggle to get out of such an environment and it takes alot of strength to do so. I think this is what the movie was really trying to convey. It ended optimistically but in no way was it a Hollywood type of ending. I liked that because it was realistic.

The teacher is played by Ryan Gosling, whom I really liked in the movie The Believer. In that movie he played a neo-Nazi, and a journalist uncovered he was actually Jewish. In the end he realized he was wrong and was filled with guilt. Supposedly it is based on a true story, but I thought he acted really good in that movie and this one.

Permalink 07:45:52 pm, Categories: Health, 73 words   English (US)

Runner's cry

Excellent article on the impact of jogging on baby boomers.

Besides I've seen many joggers, and never a happy one. So if their agonized expression is the same as the pain experienced by their joints, it's small wonder that hips and knees give out at a young age. Jogging is frequently a disaster waiting to happen as it involves intense pressure, particularly on the knee.

Really, when have you seen a smiling jogger?

Viraj's Weblog

Donate to keep this site going!

Amount USD $

September 2006
Mon Tue Wed Thu Fri Sat Sun
<<  <   >  >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  

Search

Categories


Misc

Syndicate this blog XML

What is RSS?

powered by
b2evolution