Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-24 Thread Sidharth Kuruvila
> even more optimizations can be made at runtime. Much of that is just not
> possible in Python. No amount of compiler improvements and optimizations can
> change this.

Unless someone comes up with a really smart profiling jit. Like, I
believe, they're doing with the javascript engines. :-). And given the
amount of interest in dynamically typed languages on .net and the jvm
this does seem a possiblity.

Regards,
Sidharth
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-22 Thread Sidharth Kuruvila
Hi,
  I don't think you should see and difference in performance. Lists
might take a bit more space since they usually preallocate memory for
future inserts.

  I'd go for lists where ever i need to use an array or a list, and
tuples for storing records.

Regards,
Sidharth



On Tue, Dec 22, 2009 at 7:10 PM, Vishal  wrote:
> Hi,
>
> I was presuming that since tuples are immutable, like strings, and string
> immutability increases performance (
> http://effbot.org/pyfaq/why-are-python-strings-immutable.htm)
> so also, using tuple would improve performance over Lists.
>
> is this presumption correct?
>
> if it is, then as a practice, If I know the contents of my sequence at the
> time of initialization and the fact that the sequence is not going to change
> at runtime, would it be always good to use tuples instead of lists.
>
> Any views on this one?
>
> Thanks and best regards,
> Vishal Sapre
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Python moratorium

2009-11-13 Thread Sidharth Kuruvila
This is to allow the alternate implementations to catch up, and not
really about the users of the python language. Adding breaking changes
between versions of python 3 would just be bad language design, so
users should be fine either way.

I've found myself quite happy using many of the new features that were
added to python over the years. I'll certainly miss that.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] ElementTree nodes

2009-10-25 Thread Sidharth Kuruvila
Ah yes, silly me. Must remember to go to the docs before posting :">.



On Mon, Oct 26, 2009 at 2:12 AM, bhaskar jain
 wrote:
> On Mon, Oct 26, 2009 at 1:53 AM, Sidharth Kuruvila <
> sidharth.kuruv...@gmail.com> wrote:
>>>>It turns out any object that returns len(o) as 0
>>>>will evaluate as false.
>
> Not always.
>
> doc says - "instances of user-defined classes, if the class defines a
> __nonzero__() or __len__() method, when that method returns the integer zero
> or bool value False."
>
> object.__nonzero__(self)
>
> Called to implement truth value testing and the built-in operation bool();
> should return False or True, or their integer equivalents 0 or 1. When this
> method is not defined, __len__() is called, if it is defined, and the object
> is considered true if its result is nonzero. If a class defines neither
> __len__() nor __nonzero__(), all its instances are considered true.
>
>
> First it looks for __nonzero__ and if its absent then only __len__
>
>>>> class c:
> ... def __nonzero__(self):
> ...     return False
> ... def __len__(self):
> ...    return True
> ...
>>>> o = c()
>>>> bool(o)
> False
>
>
>
>
> --Bhaskar.
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] ElementTree nodes

2009-10-25 Thread Sidharth Kuruvila
Hi,
To answer myself. It turns out any object that returns len(o) as 0
will evaluate as false.

class c:
def __len__(self):
return 0

i = c()

print bool(i)
# Prints False

On the one side this does make things quite consistent. On the other
it is a bit unintuitive. Any idea if this behaviour holds in python 3?

Regards,
Sidharth


On Mon, Oct 26, 2009 at 1:43 AM, Sidharth Kuruvila
 wrote:
> Hi,
>
>  I don't have elementree, so I just wanted to confirm what is
> happening here. Is it that the Element type is a subclass of list.
> Which would lead an empty Element to have the same boolean property as
> an empty list.
>
> Or is there some way of specifying the truthfulness of an object.
>
> Regards,
> Sidharth
>
> On Mon, Oct 26, 2009 at 1:07 AM, Noufal Ibrahim  wrote:
>> On Mon, Oct 26, 2009 at 12:52 AM, bhaskar jain
>>  wrote:
>>> On Mon, Oct 26, 2009 at 12:31 AM, Noufal Ibrahim  wrote:
>>>>>>Can you try this with an element that has zero children? From effbot's
>>>>>>docs, I think that's the difference.
>>> "The boolean interpretation will most likely change in future versions, so
>>> that all elements evaluate to true, also if they have no children."
>>> Correct.[..]
>>
>> Yup. Felt weird that an object with working methods evaluated to false
>> just because it was a leaf in a tree. That's probably saying a lot
>> about the consistency of objects in Python generally and how a small
>> thing can be quite jarring.
>>
>> --
>> ~noufal
>> http://nibrahim.net.in
>> ___
>> BangPypers mailing list
>> BangPypers@python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>
>
>
> --
> I am but a man.
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] ElementTree nodes

2009-10-25 Thread Sidharth Kuruvila
Hi,

  I don't have elementree, so I just wanted to confirm what is
happening here. Is it that the Element type is a subclass of list.
Which would lead an empty Element to have the same boolean property as
an empty list.

Or is there some way of specifying the truthfulness of an object.

Regards,
Sidharth

On Mon, Oct 26, 2009 at 1:07 AM, Noufal Ibrahim  wrote:
> On Mon, Oct 26, 2009 at 12:52 AM, bhaskar jain
>  wrote:
>> On Mon, Oct 26, 2009 at 12:31 AM, Noufal Ibrahim  wrote:
>Can you try this with an element that has zero children? From effbot's
>docs, I think that's the difference.
>> "The boolean interpretation will most likely change in future versions, so
>> that all elements evaluate to true, also if they have no children."
>> Correct.[..]
>
> Yup. Felt weird that an object with working methods evaluated to false
> just because it was a leaf in a tree. That's probably saying a lot
> about the consistency of objects in Python generally and how a small
> thing can be quite jarring.
>
> --
> ~noufal
> http://nibrahim.net.in
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] sort query

2009-10-24 Thread Sidharth Kuruvila
Hi,

 The sort method doesn't return anything. It modifies the list which
you call it on.

l = d.keys()
l.sort() #There is no point assigning the return value to this because
it will be None
#Which is what you do when you write l = d.keys().sort()
print l


Now there is another subtle issue here with d.keys(), ever time you
call it, you get a new list object, not the one you got with the
previous call. So don't expect d.keys() to return a sorted list.

Also, it's quite clear you didn't understand any of the previous mails
because Nauful did bring up the lack of a return value for sort in his
mail. If you don't understand something ask.

An important point is if you find something wrong with an api, it is
more likely that there is something wrong with your understanding than
the api itself. This is something most of us have to learn the hard
way. So do  a lot of hard thinking before you say something is broken.

Regards,
Sidharth


On Sat, Oct 24, 2009 at 4:49 PM, bhaskar jain
 wrote:
> Thanks all for replying.
>
> Let me be clear,
>
 l = [2,1]
>
 id(l[0])
> 8402300
 id(l[1])
> 8402312
>
 l.sort()
>
 id(l[0])
> 8402312
 id(l[1])
> 8402300
>
> So if we had   [l] -->  [0]  -> 2
>                                   [1]  -> 1
>
> after the sort, the index [0] binds to  the '1' memory location and index
> [1] binds to '2'.
>
>
>
> Now if we have, d = {'a':1, 'b':2}
 l = d.keys().sort()
 print l
> None
>
>
> d.keys() is a list  which references the keys of the dictionary.
> But the sort method does not do what is intended on this list?
>
> --Bhaskar.
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] sort query

2009-10-24 Thread Sidharth Kuruvila
Hi Anand.

> Looks like Python dictionary implementation is doing something clever.
> d.keys() returns the cached object if its refcount == 1 and returns a
> new object if refcount > 1.
>

I don't think that's what's happening

>>> id(d.keys())
535168
>>> id(d.keys())
535168
>>> l = [1,2,3,4]
>>> id(d.keys())
542640

It seems that when d.keys is called the second it's using the same
memory as the previous call as that one was deleted. Creating a list
displaces the memory.

Regards,
Sidharth
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] sort query

2009-10-24 Thread Sidharth Kuruvila
Hi,

 One more thing, the dict in python is a hash map so the keys won't be
ordered. If you want an ordered set of keys you could consider storing
the keys in a separate ordered list. You could also consider building
a binary tree based dict but that would be a pain.

Also a quick search brought up this http://www.python.org/dev/peps/pep-0372/.

Regards,
Sidharth

On Sat, Oct 24, 2009 at 3:23 PM, Sidharth Kuruvila
 wrote:
> Hi,
>
>>>> d = {"a":1, "b":2}
>>>> d.keys()
> ['a', 'b']
>>>> a = d.keys()
>>>> b = d.keys()
>>>> id(a)
> 542120
>>>> id(b)
> 542200
>
> So d creates a new list with each call to keys.
>
> The behavior might be different in python 3 where I hear d.keys() will
> return a set
>
> Regards,
> Sidharth
>
> On Sat, Oct 24, 2009 at 2:12 PM, Noufal Ibrahim  wrote:
>> On Sat, Oct 24, 2009 at 1:32 PM, bhaskar jain
>>  wrote:
>>> Hello,
>>>
>>>  Can sort not modify read-only location.
>>>
>>>>>> d
>>> {'a': 1, 'c': 3, 'b': 2}
>>>
>>>>>> id(d)
>>> 412816
>>>
>>>>>> id(d.keys())
>>> 404296
>>>
>>>>>> type(d.keys())
>>> 
>>>
>>>>>> print d.keys().sort()
>>
>> The sort method of a list doesn't return a sorted list. As for the
>> question of what exactly did that sort and where is the sorted list,
>> I'm not sure.
>>
>>
>> --
>> ~noufal
>> http://nibrahim.net.in
>> ___
>> BangPypers mailing list
>> BangPypers@python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>
>
>
> --
> I am but a man.
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] sort query

2009-10-24 Thread Sidharth Kuruvila
Hi,

>>> d = {"a":1, "b":2}
>>> d.keys()
['a', 'b']
>>> a = d.keys()
>>> b = d.keys()
>>> id(a)
542120
>>> id(b)
542200

So d creates a new list with each call to keys.

The behavior might be different in python 3 where I hear d.keys() will
return a set

Regards,
Sidharth

On Sat, Oct 24, 2009 at 2:12 PM, Noufal Ibrahim  wrote:
> On Sat, Oct 24, 2009 at 1:32 PM, bhaskar jain
>  wrote:
>> Hello,
>>
>>  Can sort not modify read-only location.
>>
> d
>> {'a': 1, 'c': 3, 'b': 2}
>>
> id(d)
>> 412816
>>
> id(d.keys())
>> 404296
>>
> type(d.keys())
>> 
>>
> print d.keys().sort()
>
> The sort method of a list doesn't return a sorted list. As for the
> question of what exactly did that sort and where is the sorted list,
> I'm not sure.
>
>
> --
> ~noufal
> http://nibrahim.net.in
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] An interesting beginner post at Stackoverflow

2009-10-21 Thread Sidharth Kuruvila
Hi,
  My bad, that was a bit of laziness on my part. The reason why my
code was silly is not to do with interning though that does happen for
strings. Literals, that is numbers and string literals and a few
others are loaded as constants. So the cost of constructing them in
your code has already been taken care of.

A better example would be something like

d = {"a":[1,2,3,4]}

print d.setdefault("a", [])


Interning is an optimization done to speed up the comparison of
strings, by making sure that two string with the same text are
represented by the same object.

Regards,
Sidharth


On Wed, Oct 21, 2009 at 12:31 PM, Anand Chitipothu  wrote:
>> What do you mean when you use the word "interned"?
>
> $ pydoc intern
> Help on built-in function intern in module __builtin__:
>
> intern(...)
>    intern(string) -> string
>
>    ``Intern'' the given string.  This enters the string in the (global)
>    table of interned strings whose purpose is to speed up dictionary lookups.
>    Return the string itself or the previously interned string object with the
>    same value.
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] An interesting beginner post at Stackoverflow

2009-10-20 Thread Sidharth Kuruvila
Hi,

 d = {"a":"Hello"}

 print d.setdefault("a", "blah")

 Even though the string blah is not being used an object has to be
created to represent it. Even worse, you could put some complex
expression in there expecting it to evaluate only if the key is
missing.

Regards,
Sidharth


On Wed, Oct 21, 2009 at 8:29 AM, Roshan Mathews  wrote:
> On Wed, Oct 21, 2009 at 8:06 AM, srid  wrote:
>> http://stackoverflow.com/questions/1597764/is-there-a-better-pythonic-way-to-do-this
>>
> Nice.  Martelli says:
>    (avoid setdefault, that was never a good design and doesn't have
>     good performance either, as well as being pretty murky)
>
> Any idea why?
>
>> I am now researching on a way to gather top posts (w/ python tag) on
>> Stackoverflow to create something similar to weeklyreddit.appspot.com
>>
> Weekly Reddit is such a cool idea, except I end up checking proggit
> frequently anyways.  Then I guilt out when I get the rss posts on
> Sunday.  :-S
>
> Roshan Mathews
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] help me to fix this error

2009-10-20 Thread Sidharth Kuruvila
Hi,
  Blargh! Boy! Read the mails! That code you wrote runs fine on 2.6.
But won't run on 3.0.

This should work.

intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab,outtab) #Watch this line!
st = "this is string examplewow!!!";
print(st.translate(trantab))

Basically they've moved maketrans from string to str.

Regards,
 Sidharth

On Tue, Oct 20, 2009 at 3:11 PM, Senthil Kumar M  wrote:
> from string import maketrans
> intab = "aeiou"
> outtab = "12345"
> trantab = maketrans(intab,outtab)
> st = "this is string examplewow!!!";
> print(st.translate(trantab))
> --
> I got this error,
> Traceback (most recent call last):
>   File "C:\Python30\fullpath.py", line 4, in 
>     trantab = maketrans(intab,outtab)
>   File "C:\Python30\lib\string.py", line 55, in maketrans
>     raise TypeError("maketrans arguments must be bytes objects")
> TypeError: maketrans arguments must be bytes objects
>
>
>
> On Tue, Oct 20, 2009 at 2:59 PM, Anand Balachandran Pillai
>  wrote:
>>
>>
>> On Tue, Oct 20, 2009 at 2:40 PM, Senthil Kumar M 
>> wrote:
>>>
>>> --
>>> #!/usr/bin/python
>>> from string import maketrans
>>> intab = "aeiou"
>>> outtab = "12345"
>>> trantab = intab.maketrans(outtab)
>>> str = "this is string examplewow!!!";
>>> print str.translate(trantab);
>>
>>  "print" is a function from python 3.0. Change this to,
>>  print (str.translate(trantab))
>>
>>  And you don't need semi-colons in Python. Semi-colons
>>  are used only to separate 2 expressions in the same line.
>>  Something like
>>
>>  x=2; print x
>>
>>  Here they are superfluous.
>>
>>  And I think "outtab" should be a dict, not string... but I am not sure.
>>
>>>
>>> 
>>> Thanks for the response.
>>> I changed the code as above
>>> this code too shows an error.
>>>
>>> On Tue, Oct 20, 2009 at 2:23 PM, Sidharth Kuruvila
>>>  wrote:
>>>>
>>>> Hi,
>>>> This looks like it's because python's strings have change in python 3.
>>>> The characters used to be 8bit bytes but now they are 16 bits wide.
>>>>
>>>> A quick google tells me that str now has a method called maketrans so
>>>> s1.maketrans(s2) should work.
>>>>
>>>> I'm guessing you are using a tutorial written for one of the older
>>>> versions of python. I'd suggest you either find a python 3 based
>>>> tutorial or use an older version of python, maybe python 2.6.
>>>>
>>>> ps Can you cut and paste the code into the mail next time, images are
>>>> a pain to work with.
>>>>
>>>> Regards,
>>>> Sidharth
>>>>
>>>> On Tue, Oct 20, 2009 at 1:52 PM, Senthil Kumar M 
>>>> wrote:
>>>> >
>>>> > I am using python IDLE (python3.0.1) . I dont know why this error
>>>> > comes ? I
>>>> > am a new user to python.
>>>> >
>>>> > the link of the image snapshot is
>>>> > http://img197.imageshack.us/img197/3405/pythonshell.png
>>>> >
>>>> > --
>>>> > 
>>>> > M.Senthil Kumar
>>>> > 
>>>> >
>>>> > ___
>>>> > BangPypers mailing list
>>>> > BangPypers@python.org
>>>> > http://mail.python.org/mailman/listinfo/bangpypers
>>>> >
>>>> >
>>>>
>>>>
>>>>
>>>> --
>>>> I am but a man.
>>>> ___
>>>> BangPypers mailing list
>>>> BangPypers@python.org
>>>> http://mail.python.org/mailman/listinfo/bangpypers
>>>
>>>
>>>
>>> --
>>> 
>>> M.Senthil Kumar
>>> 
>>>
>>> ___
>>> BangPypers mailing list
>>> BangPypers@python.org
>>> http://mail.python.org/mailman/listinfo/bangpypers
>>>
>>
>>
>>
>> --
>> --Anand
>>
>>
>>
>>
>> ___
>> BangPypers mailing list
>> BangPypers@python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>
>
>
> --
> 
> M.Senthil Kumar
> 
>
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] help me to fix this error

2009-10-20 Thread Sidharth Kuruvila
Hi,

Could you print the error message too. I don't have python 3 so i
can't run the code you posted.

A couple of quick pointers.

You don't need this line `#!/usr/bin/python`. That's for telling a
unix shell to use python to run the remaining code.

Yo don't need this line `from string import maketrans` because you
aren't using the string module.

Never do this `str = "this is string examplewow!!!";` You are
overwriting str which is the type object for strings in python.
Shouldn't be an issue in your code. But it's generally bad coding to
change built-in functions and variables. Use another variable name.

You can find out what methods are available on an object using the dir
function, `dir("hello")` will tell you the methods available for the
str objects. help("hello") will give you help on str objects.

Regards,
Sidharth



On Tue, Oct 20, 2009 at 2:40 PM, Senthil Kumar M  wrote:
> --
> #!/usr/bin/python
> from string import maketrans
> intab = "aeiou"
> outtab = "12345"
> trantab = intab.maketrans(outtab)
> str = "this is string examplewow!!!";
> print str.translate(trantab);
> 
> Thanks for the response.
> I changed the code as above
> this code too shows an error.
>
> On Tue, Oct 20, 2009 at 2:23 PM, Sidharth Kuruvila
>  wrote:
>>
>> Hi,
>> This looks like it's because python's strings have change in python 3.
>> The characters used to be 8bit bytes but now they are 16 bits wide.
>>
>> A quick google tells me that str now has a method called maketrans so
>> s1.maketrans(s2) should work.
>>
>> I'm guessing you are using a tutorial written for one of the older
>> versions of python. I'd suggest you either find a python 3 based
>> tutorial or use an older version of python, maybe python 2.6.
>>
>> ps Can you cut and paste the code into the mail next time, images are
>> a pain to work with.
>>
>> Regards,
>> Sidharth
>>
>> On Tue, Oct 20, 2009 at 1:52 PM, Senthil Kumar M 
>> wrote:
>> >
>> > I am using python IDLE (python3.0.1) . I dont know why this error comes
>> > ? I
>> > am a new user to python.
>> >
>> > the link of the image snapshot is
>> > http://img197.imageshack.us/img197/3405/pythonshell.png
>> >
>> > --
>> > 
>> > M.Senthil Kumar
>> > 
>> >
>> > ___
>> > BangPypers mailing list
>> > BangPypers@python.org
>> > http://mail.python.org/mailman/listinfo/bangpypers
>> >
>> >
>>
>>
>>
>> --
>> I am but a man.
>> ___
>> BangPypers mailing list
>> BangPypers@python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>
>
>
> --
> 
> M.Senthil Kumar
> 
>
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] help me to fix this error

2009-10-20 Thread Sidharth Kuruvila
Hi,
This looks like it's because python's strings have change in python 3.
The characters used to be 8bit bytes but now they are 16 bits wide.

A quick google tells me that str now has a method called maketrans so
s1.maketrans(s2) should work.

I'm guessing you are using a tutorial written for one of the older
versions of python. I'd suggest you either find a python 3 based
tutorial or use an older version of python, maybe python 2.6.

ps Can you cut and paste the code into the mail next time, images are
a pain to work with.

Regards,
Sidharth

On Tue, Oct 20, 2009 at 1:52 PM, Senthil Kumar M  wrote:
>
> I am using python IDLE (python3.0.1) . I dont know why this error comes ? I
> am a new user to python.
>
> the link of the image snapshot is
> http://img197.imageshack.us/img197/3405/pythonshell.png
>
> --
> 
> M.Senthil Kumar
> 
>
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Python interpreter in mobile phone (java)

2009-10-18 Thread Sidharth Kuruvila
Hi,

There's jython http://www.jython.org/. I don't  know if it will run on j2me.

Regards,
Sidharth

On Sun, Oct 18, 2009 at 6:38 PM, Aneesh A  wrote:
> I and my friend like to do a mobile application in python.
>
> It should be working on most of mobiles.
>
> Is there any python interpreter that written in java (so it can run on java
> supported mobiles..) for mobile??
>
> I have seen python interpreter in my friend's nokia mobile. but it works
> only on symbian.
>
> --
> +91 903 755 72 73
>
> For all hardware and software services, Computer assembling, Software
> Installation etc.
> My blog : http://xtenders.blogspot.com/
>
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


[BangPypers] Introducing the Y Combinator(Not the company)

2009-10-15 Thread Sidharth Kuruvila
AKA The Y Combinator in python. This is in response to Roshan Mathews'
post that he got stuck with the Y Combinator.

Aha a challenge, I shall undertake this as a test of my communication
skillz. :-)

The question is how do we implement a recursive function in a language
in which names can only be bound using a lambda expression eg. Python
without assignment operators and def.

We can solve this problem fairly easily, we pass the function itself
as a parameter. So now any recursive call must include the function as
a parameter too.

fact = lambda _fact, x: 1 if x == 1 else x * _fact(_fact, x-1)

print fact(fact, 5)

Or course lambda expressions in python are famously hard to read. So
we'll allow `def` with some restrictions. The code block of a function
definition cannot contain a call to the function itself. This is
because the function name isn't bound at the time the function itself
is defined.

We can rewrite the lambda code as

def fact(_fact, x):
if x == 1:
return 1
else:
return x*_fact(_fact, x-1)

print fact(fact, 5)

There, thats a lot more readable. Having to type `_fact(_fact, x-1)`
is quite irritating especially if the recursive call is made in more
than one place. So we can extract it into a function `f`.

def fact(_fact, x):
def f(x):
return _fact(_fact, x)

if x == 1:
return 1
else:
return x * f(x-1)

print fact(fact, 5)

But we still need to define `f` at the top of all our functions. It
makes sense to generalize this pattern so that `f` itself can be
passed as a parameter to fact. This is where the y combinator comes
in.

Ok, now things start to get a bit weird so hang in there.

Here's a definition of y combinator using lambdas, it's not
particularly readable so we'll just ignore it.

def y(f):
return ((lambda g: lambda a: f(g(g), a))
(lambda g: lambda a: f(g(g), a)))

Let's implement the y combinator using `def` instead.

def y(f):
def _y(g):
def _f(a):
return f(g(g), a)
return _f
return _y(_y)

`_f` accepts parameter `a` and then calls `f` with the parameters
`g(g)` and `a`.  `_y(_y)` bootstraps the whole operation by passing
`_y` to itself to be bound to `g`. `g` is now `_y`, so a call to `g`
will return `_f`.

So now we can implement fact to use the y combinator.

def fact(f, x):
if x == 1:
return 1
else:
return x * f(x-1)

fact = y(fact)

print fact(5)

ps. I wrote a lisp interpreter using lambda and the bootstrapping
concept some time ago. It's a bit of a brain damaged ugly sister of
the the y combinator.

http://kuruvila.net/2008/02/29/a-one-line-lisp-interpreter/

-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Pickle multiple objects

2009-10-07 Thread Sidharth Kuruvila
Oops formatting got mucked up. Should be

def pickledobjects(f):
try:
while True:
yield pickle.load(f)
except EOFError:
pass

objs = list(pickledobjects(file("fi")))
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Pickle multiple objects

2009-10-07 Thread Sidharth Kuruvila
Hi,
I'm guessing you want to do something like this


>>> fo = file("test.pkl", "w")
>>> pickle.dump([1,2,3,4], fo)
>>> pickle.dump([5,6,7,8], fo)
>>> fo.close()
>>> fi = file("test.pkl")
>>> pickle.load(fi)
[1, 2, 3, 4]
>>> pickle.load(fi)
[5, 6, 7, 8]
>>> pickle.load(fi)

Traceback (most recent call last):
File "", line 1, in 
pickle.load(fi)
File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pickle.py",
line 1370, in load
return Unpickler(file).load()
File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pickle.py",
line 858, in load
dispatch[key](self)
File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pickle.py",
line 880, in load_eof
raise EOFError
EOFError

You will need to handle the EOFError in a try catch block.

maybe

def pickledobjects(f):
try:
while True:
yield pickle.load(f)
except EOFError:
pass

objs = list(pickledobjects(file("fi")))

Though, is this isn't a large amount of data, I'd recommend just storing
the data in a single object and writing that to the file.

On Wed, Oct 7, 2009 at 1:14 PM, Aneesh A  wrote:

> Hi friends,
>I am new to python world. I am doing a small python game ( command line
> based). The problem is:
>
> I have to store high scores, so i pickled a list . after pickling, in
> append mode, load method loads only first object.
>
> How to retrieve multiple objects??
>
> I am attaching a source.
>
> Look the alpha.
>
>


-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] SciPy India 2009 - SciPy.in

2009-10-04 Thread Sidharth Kuruvila
A thousand apologies, the "possibly just for Pradeep, goto", was a meant  
as a light joke not to be taken personally. Goto was the original issue  
with Basic.


You hit the nail squarely on the head when you say "The *users* of PHP  
tend to get wired in weird ways after using it". So do the users of python  
or any other language, in their own way. You can choose not to like Php,  
but there are a lot of good programmers who do use it productively.


The fact is that you've obviously invested some amount of time working  
with various python frameworks, which make certain choices obvious to you.  
The author of the article obviously wasn't able to do that, Drupal seems  
like a sensible decision to have made. I'd agree the post is almost  
certainly not objective, but then most comparisons are that way, it's  
something we have to live with.


Ps. I am talking from my own perspective, if I was asked to create a  
website backed by a database, I'd chose something based on Php simply  
because that's what I have some recent experience working with, so I spend  
less time wasted trying to learn the tools.

___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] SciPy India 2009 - SciPy.in

2009-10-04 Thread Sidharth Kuruvila
Hey Hey, no need to dis Php, or Drupal for that matter, it's actually a  
mighty fine language for what it does. And they have been making it a lot  
better(namespaces, closures and, possibly just for Pradeep, goto).


Nice to see conferences happening around python.

On Sun, 04 Oct 2009 11:47:27 +0530, Pradeep Gowda   
wrote:


On Sun, Oct 4, 2009 at 2:03 AM, Kenneth Gonsalves   
wrote:

On Sunday 04 Oct 2009 11:22:28 am Anand Balachandran Pillai wrote:

> >  6 days... isn't it rather too long for a specific interest
> >  conference.
>
> weird: http://fossee.in/whydrupal

  Hadn't noticed it was written in Drupal. Well, they might have an
  excuse since they are focusing on "Scipy" rather than just Py!


I am quite annoyed by the FUD about plone - granted plone is a pain to
customise, but a Q&D CMS? All it needs is click click click - what on  
earth is
this guy doing with buildout, restarting zope etc etc? Not that I would  
expect

much from someone who is comfortable in drupal. SciPHP anyone?


Plone has the easiest setup story of all the CMSes. You don't even
need to have a database  and a webserver for $deity's sake. Some how
installing and configuring a database, and a webserver is easier than
running an installer !

I think the key quote from the TFA is "Since
I had some experience with drupal, I suggested that we go with drupal,
since I had some experience with its working. I worked for a couple of
hours to set up a basic site on my local machine"

Yeah? really? what about downloading a windows installer or a mac
installer  for plone and doing a click-click-click as KG suggested
and be done with it in minutes?

The biggest joke of all has to be the fact that all the features that
I see on their site, including events works out of the box on plone
without installing a single external plugin. And plone's event
features have lot more features than the half-assed calender i see on
that site.

PHP+MySQL is the BASIC of this age.

+PG
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Sidharth Kuruvila

This should work,

find f(6)**(10 raised to 7)



On Mon, 14 Sep 2009 18:36:57 +0530, Shashwat Anand  
 wrote:



How do we calculate last 5-digits of 10**12 ignoring trailing zeros. The
code i wrote works good until 10**8 and after that take ages.
The source of problem is Project Euler :
http://projecteuler.net/index.php?section=problems&id=160

The code is pasted here : http://paste.pocoo.org/show/139745/

 1 '''


  2 For any N, let f(N) be the last five digits before the trailing  
zeroes

in N!.
  3 For example,
  4
  5 9! = 362880 so f(9)=36288
  6 10! = 3628800 so f(10)=36288
  7 20! = 243290200817664 so f(20)=17664
  8
  9 Find f(1,000,000,000,000)
 10 '''
 11 def f(n):
 12 fac = 1
 13 i = 1
 14 #for i in range(1, n+1):
 15 while i < n + 1:
 16 fac = int(str(fac * i).strip('0')) % 10
 17 i += 1
 18 return fac
 19
 20 print f(1)

PS. hope posting algorithmic doubts will not be considered spamming :)



--
From my desktop to yours.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Low level Python

2009-09-12 Thread Sidharth Kuruvila

What's on your mind?

On Sat, 12 Sep 2009 01:51:57 +0530, Noufal Ibrahim   
wrote:



Hello everyone,
Are there any people here who are interested and who've worked on
the actual CPython (or any other) interpreter directly? The whole idea
of unladen swallow is appealing to me and if there are others who are
into that kind of thing, it'd be great to collaborate on something on
that front.






--
From my desktop to yours.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] How to create Debug and Release code in Python

2009-06-07 Thread Sidharth Kuruvila
Hi,

You could try the C preprocessor.  Personally, I'd just go for python's
inbult logging framework, and worry about the performance issue later.

Regards



On Mon, Jun 8, 2009 at 1:07 AM, Vishal  wrote:

> Hello,
> We would like to have debug and release versions of our scripts. However
> since the scripts are directly used by the 'users' and they are not real
> software guys...maintaining two different scripts and then keeping them
> synchronized across 15-20 workstations etc is not something that the
> management wants.
> Scripts are complex enough that we cannot abstract out all the variables
> into config fileswhich might have been easier for users (just change
> parameters in a config file)
>
> *Is there a way to create a conditionally compilable Python script ?* Some
> facility that would prevent from code getting compiled into
> ".pyc"something like the old #ifdef #endif preprocessor directivesis
> there a Python preprocessory available :)
>
> if thats doable I can ask them to make all experimental modifications
> within the conditional directives. I could still ask them to do that using
> simple if's and bool parameters, however, putting too many ifs might degrade
> the performance of these scripts. ("the lesser the branches the better it is
> for processor performance...more so for super scalar processors")
>
> We cannot use the, often unknown, __debug__ flag, because our users would
> like to simply double-click on a python file, which means to use the -O or
> -OO options (which sets __debug__ to False) we'd have to make windows python
> command line have these, and then double clicking would be of no use if you
> want the run stuff inside the __debug__.
>
> Any and every help is most welcome :)
>
> Thanks and best regards,
> Vishal Sapre
>
> On Wed, May 27, 2009 at 1:25 PM, Sam's Lists  wrote:
>
>> Anand---
>>
>> Thanks, that worked great.
>>
>> -Sam
>>
>>
>> On Tue, May 26, 2009 at 7:34 PM, Anand Chitipothu 
>> wrote:
>>
>>> > text = "The Price £7"
>>> > pattern = u"£\d"
>>> >
>>> > m = re.search(pattern, text, re.UNICODE)
>>> > print m.group(0)
>>>
>>> Your text is in utf-8 encoding and pattern in unicode.
>>> Make text unicode solves the issue.
>>>
>>> text = u"The Price £7"
>>> pattern = u"£\d"
>>> m = re.search(pattern, text, re.UNICODE)
>>> print m.group(0).encode('utf-8')
>>>
>>> Anand
>>> ___
>>> BangPypers mailing list
>>> BangPypers@python.org
>>> http://mail.python.org/mailman/listinfo/bangpypers
>>>
>>
>>
>> ___
>> BangPypers mailing list
>> BangPypers@python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>>
>
>
> --
> Thanks and best regards,
> Vishal Sapre
>
> ---
>
> "So say...Day by day, in every way, I am getting better, better and better
> !!!"
> "A Strong and Positive attitude creates more miracles than anything else.
> Because...Life is 10% how you make it, and 90% how you take it"
> "Diamond is another piece of coal that did well under pressure”
> "Happiness keeps u Sweet, Trials keep u Strong,
> Sorrow keeps u Human, Failure Keeps u Humble,
> Success keeps u Glowing, But only God Keeps u Going.Keep Going."
>
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>


-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] help using mysqldb

2008-12-17 Thread Sidharth Kuruvila
Do this instead

cur.execute('select * from Employee where id in (%s)', ids.join(', '));

On Wed, Dec 17, 2008 at 5:13 PM, Vijay Ramachandran wrote:

> Hello.
>
> I'm unable to figure out how to use mysqldb (actually, dbabi) to write an
> "in" query.
>
> For instance, suppose my table looks like this:
> Employee(
>  id int not null primary key,
>  name varchar(32) not null)
> );
>
> Suppose I have a list of ids, I would write sql such as
> 'select * from Employee where id in (id1, id2, id3)' and this would work
> even if there was only one id, say, 'select * from Employee where id in
> (id1)'
>
> How should I do this in MySQLdb? It seems that I need to handle single
> entry list differently from a list with multiple elements? i.e., this code
> below works:
>
>
> ids = [1, 2, 3]
> if len(ids) > 1:
> cur.execute('select * from Employee where id in %s', tuple(ids))
> else:
> cur.execute('select * from Employee where id = %s', ids[0])
>
> but it doesn't seem right. What's the correct way to run this query?
>
> thanks,
> Vijay
>
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>


-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] help needed with dictionary

2008-08-29 Thread Sidharth Kuruvila
Hi Sonny,

Not if you run kev={} in front of your code. It will keep getting
reinitialized to an empty dict.



On Fri, Aug 29, 2008 at 3:42 PM, sunny_plone <[EMAIL PROTECTED]> wrote:

>
> hi ,
> actually my main problem is the the key name or Name should store multiple
> values, but now the old value gets replced wen new value is entered. how
> can
> i store old n new values for same key.
>
> Anand-17 wrote:
> >
> > On Fri, Aug 29, 2008 at 3:27 PM, sunny_plone <[EMAIL PROTECTED]>
> wrote:
> >>
> >> hi,
> >> sorry for late reply.
> >> i want to create a address book which has fields like name, phno, email
> >> id
> >> etc which are keys and i want each key to hav multiple values as
> >> many users will enter their names. so name key will contain many values.
> >> i
> >> am not able to store the values entered through command line. i am able
> >> to
> >> store 1st value, but wen sec value is entered first one is erased.
> >
> > Shouldn't the code be this?
> > key[name].append(person_name)
> >
> > Looks like you are always using the same key "Name".
> > ___
> > BangPypers mailing list
> > BangPypers@python.org
> > http://mail.python.org/mailman/listinfo/bangpypers
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/help-needed-with-dictionary-tp19215907p19217224.html
> Sent from the BangPypers - Bangalore Python Users Group mailing list
> archive at Nabble.com.
>
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] strange behavior

2008-07-10 Thread Sidharth Kuruvila
Any number with a decimal point is treated as an object of type float and
the numbers without a decimal point of are treated as ints. You can use the
type function to find the type of an object.

>>> type(1)

>>> type(1.0)


>>> 1+1.0
2.0
Arithmetic operations over two types give the result in the more flexible
type, in this case that is a float.

You can use the int and float functions for type conversion

>>> int(1.0)
1
>>> float(1)
1.0


On Thu, Jul 10, 2008 at 10:30 AM, Pradeep Gowda <[EMAIL PROTECTED]> wrote:

>
> On Jul 10, 2008, at 12:54 AM, Gopal Ghosh wrote:
>
>>
>>
>> *
>> >>> 7/3
>> 2
>> >>> 7/-3
>> -3
>> >>> 3/7
>> 0
>> >>> # again reapting the questions with one more decimal place
>> >>> 7.0/3
>> 2.3335
>> >>> 7.0/-3
>> -2.3335
>> >>> 3.0/7
>> 0.42857142857142855
>> >>> # why it is not showing the exact values in the previous cases
>> >>>
>>
>
> Dividing Integer by an integer will give you Integer
> Dividing Integer by a float or a float by an integer will give a float.
> See this: http://docs.python.org/ref/binary.html
>
> However this behaviour will change to a less "surprising"   3/4 returning
> 0.75 as one would expect in python 3.0
> See: http://www.comp.leeds.ac.uk/nde/papers/
> teachpy3.html#unsurprising-arithmetic
>
> -PG
> http://pradeepgowda.com
>
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] [Job] Looking for developer

2008-07-05 Thread Sidharth Kuruvila
I'm sure there a lot of guys in bangpypers who could fit that bill. I don't
think you have to have all of the qualification in point one.

What I'd like to know is how successful people who post job requirements on
bangpypers have been with recruitment. Vijay?


On Sat, Jul 5, 2008 at 3:40 PM, Anand Balachandran Pillai <
[EMAIL PROTECTED]> wrote:

> I know two guys who might match these requirements. The first guy's
> name is Sergey Brinn and the second one's name is Larry Page... But it
> might be difficult to persuade them to join...
>
>
>
> On 7/4/08, Venkatraman S <[EMAIL PROTECTED]> wrote:
> > On Fri, Jul 4, 2008 at 10:50 AM, Kenneth Gonsalves <[EMAIL PROTECTED]>
> >>> wrote:
> >>>
>  if you find this guy, let me know - I would like to shake his hand ;-)
> 
>  On 03-Jul-08, at 7:36 PM, Vijay Ramachandran wrote:
> 
>   Must have
> > PhD, MS + 2years, or BE + 4years (or BE + 2 years developing web
> apps)
> > Good problem solving, analysis, algorithm skills
> > Sound knowledge of statistics, probability and related math
> > Must be an expert python hacker, who enjoys coding
> > Experience with web technologies - http, web servers (apache,
> mod_wsgi,
> > mod_python), frameworks (pylons, django), and processing html and xml
> > documents
> >
> 
> > I will prostrate ... Indian traditions :)
> >
> > Venkat
> > Blog @ http://blizzardzblogs.blogspot.com
> >
>
> --
> Sent from Gmail for mobile | mobile.google.com
>
> -Anand
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Expressing meta-data about classes, methods, variables, parameters in python

2008-04-03 Thread Sidharth Kuruvila
Hi,

You can simulate annotation using decorators. I might be missing  
something about annotation but you can do this.

def annotate(returns, **params):
 def decorate(func):
 params['returns'] = returns
 func.__annotations__ = params
 return func
 return decorate

@annotate("returns", a="a")
def f(a):
 return a

print f.__annotations__




On Apr 3, 2008, at 4:34 PM, Heshan Suriyaarachchi wrote:

> Hi Anand,
>This is what I exactly mean. This is the thing I want to do. I am  
> now using python 2.5.1 . So is
>there a way that I can do a thing like this in python 2.5.1 ( an  
> alternative way).
>
>   Heshan Suriyaarachchi
>
>
> >>> def f(x : "an integer", y: "another integer") -> "Sum of x and y":
> ... return x + y
> >>> f.__annotations__
> {'y': 'another integer', 'x': 'an integer', 'return': 'Sum of x and  
> y'}
>
> The help() function returns the annotation plus the docstring now.
> >>> f.__doc__ = "A sum function"
> >>> help(f)
> Help on function f in module __main__:
>
> f(x: 'an integer', y: 'another integer') -> 'Sum of x and y'
>A sum function
>
>
>
>
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers

___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Expressing meta-data about classes, methods, variables, parameters in python

2008-04-03 Thread Sidharth Kuruvila
H Heshan,

  Can you explain why you'd want to do this?

You can write

def f():
 print f.a

f.a = 4

f()

But I can't see you would want to do something like that. Maybe if you  
give a use case. Someone can come up with a solution.

Regards,
Sidharth

On Apr 3, 2008, at 2:03 PM, Heshan Suriyaarachchi wrote:

> Hi
> What I meant was a situation like this.
>
> In javascript I'm used to associating metadata with a function as
> follows.
>
> foo.bar = "foobar";
> function foo(){
> }
>
> What is the normal python practice to do this kind of a thing.
>
> Your help is very much appreciated.
>
> Thanks,
> Heshan Suriyaarachchi
> ___
> BangPypers mailing list
> BangPypers@python.org
> http://mail.python.org/mailman/listinfo/bangpypers

___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers