Re: python vs. grep

2008-05-06 Thread Wojciech Walczak
2008/5/6, Anton Slesarev <[EMAIL PROTECTED]>:
>  But I have some problem with writing performance grep analog.
[...]
>  Python code 3-4 times slower on windows. And as I remember on linux
>  the same situation...
>
>  Buffering in open even increase time.
>
>  Is it possible to increase file reading performance?

The best advice would be not to try to beat grep, but if you really
want to, this is the right place ;)

Here is my code:
$ cat grep.py
import sys

if len(sys.argv) != 3:
   print 'grep.py  '
   sys.exit(1)

f = open(sys.argv[2],'r')

print ''.join((line for line in f if sys.argv[1] in line)),

$ ls -lh debug.0
-rw-r- 1 gminick root 4,1M 2008-05-07 00:49 debug.0

---
$ time grep nusia debug.0 |wc -l
26009

real0m0.042s
user0m0.020s
sys 0m0.004s
---

---
$ time python grep.py nusia debug.0 |wc -l
26009

real0m0.077s
user0m0.044s
sys 0m0.016s
---

---
$ time grep nusia debug.0

real0m3.163s
user0m0.016s
sys 0m0.064s
---

---
$ time python grep.py nusia debug.0
[26009 lines here...]
real0m2.628s
user0m0.032s
sys 0m0.064s
---

So, printing the results take 2.6 secs for python and 3.1s for original grep.
Suprised? The only reason for this is that we have reduced the number
of write calls in the python example:

$ strace -ooriggrep.log grep nusia debug.0
$ grep write origgrep.log |wc -l
26009


$ strace -opygrep.log python grep.py nusia debug.0
$ grep write pygrep.log |wc -l
12


Wish you luck saving your CPU cycles :)

-- 
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list


Re: #!/usr/bin/env python vs. #!/usr/bin/python

2008-05-06 Thread Wojciech Walczak
2008/5/6, Ben Finney <[EMAIL PROTECTED]>:
>  > >  So why not put symlink to Python over there on all machines, if
>  > >  we can put one (or env itself) there ?
> > To avoid linking all the rest of interpreters like perl, ruby, lua
>  > and dozens of others.
> The argument was being made from "thousands of scripts". Isn't "dozens
>  of symlinks" better?

I think that depending on /usr/bin/env is more farsighted and saves some future
headaches. Creating links in /usr/bin/ means, that you have to change them
whenever you update your software (e.g. any of your many interpreters ;-)).
Changing the "#!/usr/bin/python" into "#!/usr/bin/env python" means that you do
your job once, and you can sleep well. It also is more portable.

How was it in perl?
perl -p -i -e 's/#\!\/usr\/bin\/python/#\!\/usr\/bin\/env python/' *.py

Funny thing, I have just ls'ed /usr/bin/python on my system:
$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 24 2007-11-16 14:02 /usr/bin/python ->
/usr/local/bin/python2.5

:-)

-- 
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list


Re: #!/usr/bin/env python vs. #!/usr/bin/python

2008-05-06 Thread Wojciech Walczak
2008/5/6, Banibrata Dutta <[EMAIL PROTECTED]>:
>  > Use /usr/bin/env.  If env is not in /usr/bin, put a link to it there.
>
>  So why not put symlink to Python over there on all machines, if we can
>  put one (or env itself) there ?

To avoid linking all the rest of interpreters like perl, ruby, lua and dozens
of others.

-- 
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Decimal vs Float comparasion

2008-05-05 Thread Wojciech Walczak
2008/5/6, Wojciech Walczak <[EMAIL PROTECTED]>:
>  a > 9.0 returns True because NotImplemented > 9.0 returns True.
>  a < 9.0 returns False because NotImplemented < 9.0 returns False.

Sorry, it should rather be:

Decimal('0.5') > 9.0 returns True because:
Decimal('0.5') > NotImplemented returns True

and:

Decimal('0.5') < 9.0 returns False because:
Decimal('0.5') < NotImplemented returns False

-- 
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Decimal vs Float comparasion

2008-05-05 Thread Wojciech Walczak
2008/5/6, Yuan HOng <[EMAIL PROTECTED]>:
>  It seems decimal object will always be larger than float in
>  comparasion, which goes against common sense:
>
>  >>> from decimal import Decimal
>  >>> a = Decimal('0.5')
>  >>> a > 9
>  False
>  >>> a > 9.0
>  True
>
>  It seems to me that rather than allowing this to happen, comparasion
>  between the two should either be made correct (by convertion decimal
>  to float e.g.) or forbidden, like arithmatic operations between the
>  two types.

Looks like a nasty bug.

a > 9.0 returns True because NotImplemented > 9.0 returns True.
a < 9.0 returns False because NotImplemented < 9.0 returns False.

As you can see the real comparision has nothing to do with your Decimal number.
I think you can report it at bugs.python.org.

-- 
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list


Re: pygame music, cant read mp3?

2008-05-05 Thread Wojciech Walczak
2008/5/5, globalrev <[EMAIL PROTECTED]>:
> pygame.mixer.music.load('C:/Python25/myPrograms/pygameProgs/example1.mp3')


Are you sure that:

os.path.exists('C:/Python25/myPrograms/pygameProgs/example1.mp3') == True?

Check it with python.

-- 
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list


Re: confused about self, why not a reserved word?

2008-05-05 Thread Wojciech Walczak
2008/5/5, globalrev <[EMAIL PROTECTED]>:
> class Foo(object):
> def Hello(self):
> print "hi"
>
>  object is purple, ie some sort of reserved word.
>
>  why is self in black(ie a normal word) when it has special powers.
>  replacing it with sel for example will cause an error when calling
>  Hello.

Could you give an example of such an error?

-- 
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Determine socket family at runtime

2008-05-04 Thread Wojciech Walczak
2008/5/4, Giampaolo Rodola' <[EMAIL PROTECTED]>:
>  For now I've been able to determine the family by using:
>
>  # self.socket = a connected socket.socket instance
>  ip, port = self.socket.getsockname()[0:2]
>  af = socket.getaddrinfo(ip, port)[0][0]
>
>  ...but I'd like to know if some other solution is preferable.

Nope, there is none. Using getaddrinfo() to check address family
is the de facto standard.

-- 
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list