more efficient?

2009-12-21 Thread Zubin Mithra
I have the following two implementation techniques in mind.

def myfunc(mystring):
check = "hello, there " + mystring + "!!!"
print check


OR
structure = ["hello, there",,"!!!"]
def myfunc(mystring):
structure[2] = mystring
output = ''.join(mystring)

i heard that string concatenation is very slow in python; so should i
go for the second approach? could someone tell me why? Would there be
another 'best-practice-style'?
Please help. Thankx in advance!

cheers!!!
Zubin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: more efficient?

2009-12-22 Thread Zubin Mithra
thank you for your help and support. i`ll keep your advice in mind. :)

cheers!!!
Zubin



On Tue, Dec 22, 2009 at 8:07 PM, Lie Ryan  wrote:
> On 12/22/2009 5:13 PM, Zubin Mithra wrote:
>>
>> I have the following two implementation techniques in mind.
>>
>> def myfunc(mystring):
>>     check = "hello, there " + mystring + "!!!"
>>     print check
>>
>>
>> OR
>> structure = ["hello, there",,"!!!"]
>> def myfunc(mystring):
>>     structure[2] = mystring
>>     output = ''.join(mystring)
>>
>> i heard that string concatenation is very slow in python; so should i
>> go for the second approach? could someone tell me why? Would there be
>> another 'best-practice-style'?
>> Please help. Thankx in advance!
>
> Python's strings are immutable and to concatenate two string the interpreter
> need to copy two whole string into a new string object. This isn't a
> performance problem until you're trying to concatenate a list containing a
> thousand strings:
> ['abc', 'bcd', 'cde', 'def', ...]
> with the naive approach:
> joined = ''
> for s in lst:
>    joined = joined + s
>
> first python will conc. '' and 'abc', copying 0+3 = 3 chars
> then it conc. 'abc' and 'bcd', copying 3+3 = 6 chars
> then it conc. 'abcbcd' and 'cde' copying 6+3 = 9 chars
> then it conc. 'abcbcdcde' and 'def' copying 9+3 = 12 chars
> and so on...
>
> for four 3-letter strings, python copies 3 + 6 + 9 + 12 = 30 chars, instead
> of the minimum necessary 12 chars. It gets worse as the number of strings
> increases.
>
> When you concatenate two 1000-chars large strings, both + and ''.join will
> have to copy 2000 chars. But when you join one thousand 2-chars string
> you'll need to copy 1001000 chars[!] with +.
>
> Now, early optimization *is evil*. Don't start throwing ''.join every here
> and there. The performance by the concatenations won't start to matter until
> you're concatenating a large lists (>40) and + is much more readable than
> ''.join().
>
> When concatenating small number of strings I preferred
> %-interpolation/str.format; it's often much more readable than ''.join and
> +.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


code review

2009-12-23 Thread Zubin Mithra
Hello,

I`m pretty new to developing applications using python and i would
like advance on this script i recently created which helps linux
programmers on the linux terminal. The code is very simple to
understand(i hope) and serves a simple purpose; however i am not sure
if my way of scripting is the right "pythonic" way of doing it.

The package can be found at www.code.google.com/p/pyautorun

I`d love any feedback, ideas and criticism after a code review. Thank
you in advance.

cheers!!!
Zubin Mithra
-- 
http://mail.python.org/mailman/listinfo/python-list


socket programming

2009-12-30 Thread Zubin Mithra
The code snippet i have pasted at,

http://paste.pocoo.org/show/160555/

produces a traceback which also has been pasted at the above link. The
snippet attempts at socket programming using OOPs concepts. Please help.

Thankx in advance
Zubin Mithra
-- 
http://mail.python.org/mailman/listinfo/python-list


PyAutoRun

2010-02-26 Thread Zubin Mithra
Hello,

I have been using python for quite some time; however this is the
first python project i have worked on.

The code is hosted at http://github.com/zubin71/PyAutoRun

The code needs re-factoring and feature additions; i have put up a
TODO list there too. It`d be great if anyone could work on this; i
intend to develop this further(with a bit of help) and will request
for its addition into debian and ubuntu repositories, in time.

Also, any kind of code-review, criticism, is also appreciated.
However, it`d be awesome if you could just fork it at github, pull,
modify and push. :)

Have a nice day!

cheers!!!
Zubin
-- 
http://mail.python.org/mailman/listinfo/python-list


D-Bus

2010-03-17 Thread Zubin Mithra
Hello,

I have experience writing scripts to connect to the D-Bus interface provided
by different applications but have no experience writing a D-Bus interface
for an application. Could someone point me in the right direction? A
tutorial or a good example would be nice.

Cheers!!!
Zubin Mithra

http://zubin71.wordpress.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Hwy doesn't len(None) return zero ?

2010-06-30 Thread Zubin Mithra
On Thu, Jul 1, 2010 at 12:09 AM, Stef Mientki wrote:

>  hello,
>
> I've lot of functions that returns their result in some kind of tuple /
> list / array,
> and if there is no result, these functions return None.
> Now I'm often what to do something if I've more than 1 element in the
> result.
> So I test:
>
>if len ( Result ) > 1 :
>
> But to prevent exceptions, i've to write ( I often forget)
> if Result and ( len ( Result ) > 1 ) :
>

use
if Result:
   

Checking the length would be a bad idea.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Python for web applications

2010-06-30 Thread Zubin Mithra
On Wed, Jun 30, 2010 at 11:34 PM, Wyatt Schwartz wrote:

> Dear Python-List members,
>
> Sorry for asking such a simple (or possibly complicated) question, as I am
> new to Python programming. Anyways, I have read online that many popular
> websites use Python for some of their web-based applications (for example,
> Reddit), and that lead me to wonder how is this done?
>
> Thanks in advance!
>
> I would recommend that you check out the various web-frameworks which are
out there. Django is the framework I use and love, but there are loads out
there.

Have fun!
Zubin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Composition of functions

2010-06-30 Thread Zubin Mithra
Hello,

>>> y=list(x).reverse()
> >>> print y
> None
>

>>> L = ["a", "b", "c"]
>>> L.reverse()
>>> L
["c", "b", "a"]

As you can see, L.reverse() performs the operation on itself and returns
nothing. Hence, the return type None.

Instead of

y=''.join(list(x).reverse())

you should probably do,

>>> t = list(x).reverse()
>>> y = ''.join(t)

Cheers!
Zubin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Composition of functions

2010-06-30 Thread Zubin Mithra
> Er, I don't think you thought that one entirely through (/ tried it out):
>
>
My Apologies.

Here is a working one.

 >>> x="123"
>>> t = list(x)
>>> t.reverse()
>>> print ''.join(t)
321


But of course, the method which was suggested earlier is far more elegant.

>>> print ''.join(reversed(list(x)))

Cheers!
Zubin
-- 
http://mail.python.org/mailman/listinfo/python-list