By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,935 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,935 IT Pros & Developers. It's quick & easy.

Better writing in python

P: n/a
I'm just wondering, if I could write a in a "better" way this code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...

Oct 24 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
On Wed, 24 Oct 2007 12:09:40 +0000, Alexandre Badez wrote:
I'm just wondering, if I could write a in a "better" way this code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...
Drop the prefixes. `l` is for list? `d` is for what!? Can't be
dictionary because the code doesn't make much sense.

Where is `cls` coming from?

Ciao,
Marc 'BlackJack' Rintsch
Oct 24 '07 #2

P: n/a
On Oct 24, 1:09 pm, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...
import operator
return filter(cls.dArguments), filter(operator.not_, cls.dArguments)

Or just:

mandatory = [arg for arg in cls.dArguments in arg]
optional = [arg for arg in cls.dArguments in not arg]
return mandatory, optional

--
Paul Hankin

Oct 24 '07 #3

P: n/a
On Oct 24, 1:09 pm, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...
import operator
return filter(cls.dArguments), filter(operator.not_, cls.dArguments)

Or just:

mandatory = [arg for arg in cls.dArguments in arg]
optional = [arg for arg in cls.dArguments in not arg]
return mandatory, optional

--
Paul Hankin

Oct 24 '07 #4

P: n/a
On Oct 24, 1:09 pm, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...
import operator
return filter(cls.dArguments), filter(operator.not_, cls.dArguments)

Or just:

mandatory = [arg for arg in cls.dArguments in arg]
optional = [arg for arg in cls.dArguments in not arg]
return mandatory, optional

--
Paul Hankin

Oct 24 '07 #5

P: n/a
On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...
You might look into list comprehensions. You could probably do this
with two of them:

<code>
# completely untested
lMandatory = [arg for arg in cls.dArguments if arg is True]
lOptional = [arg for arg in cls.dArguments if arg is False]
</code>

Something like that. I'm not the best with list comprehensions, so I
may have the syntax just slightly off. See the following links for
more information:

http://www.secnetix.de/olli/Python/l...ehensions.hawk
http://docs.python.org/tut/node7.html

Mike

Oct 24 '07 #6

P: n/a
On 2007-10-24, Alexandre Badez <al*************@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this
code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...
I don't think there's much improvement to be made. What are your
particular concerns? Is it too slow? Too verbose? Does it not
work correctly?

It appears to be an application of itertools.groupby, but that'll
almost certainly be less efficient. To use groupby, you have to
sort by your predicate, and build lists to save results. Your for
loop avoids the sorting.

Here's some instrumentality for ya:

from itertools import groupby
from operator import truth
from collections import defaultdict

result = defaultdict(list)
for k, g in groupby(sorted(cls.dArguments, key=truth), truth):
result[k].extend(g)

I like your initial attempt better.

P.S. I didn't realize until working out this example that extend
could consume an iterator.

--
Neil Cerutti
The word "genius" isn't applicable in football. A genius is a guy like Norman
Einstein. --Joe Theisman
Oct 24 '07 #7

P: n/a
Hi Alexandre,

On Oct 24, 2:09 pm, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this code
Please tell us, what it is you want to achieve. And give us some
context
for this function.
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)
This snippet will seperate d.Arguments into two lists: one that
holds all elements that are references to True(!) and another list
that holds the rest. The condition 'if args is True' will only
be hold if arg actually _is_ the object _True_. This is probably
not what you want. Apart from that, you might go with

lMandatory = [ arg for arg in cls.dArguments if condition() ]
lOptional = [ arg for arg in cls.dArguments if not condition() ]

the second line could be rewritten

lOptional = list(set(cls.dArguments)-set(lMandatory))

If lMandatory and lOptional do not need to be lists, you can also
write

lMandatory = set(arg for arg in cls.dArguments if condition())
lOptional = set(cls.dArguments) - lMandatory

But please, give us some more context of what you want to do.

- harold -

Oct 24 '07 #8

P: n/a
Thanks for your try Cliff, I was very confused :P
More over I made some mistake when I post (to make it easiest).

Here is my real code:

with
dArguments = {
'argName' : {
'mandatory' : bool, # True or False
[...], # other field we do not care here
}
}

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if cls.dArguments[arg]['mandatory']:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

So, as you see, we agree each other about "if bool" or "if bool is
True" ;)

So, my question was how to give it a better 'python like' look ?
Any idea ?

Oct 24 '07 #9

P: n/a
Alexandre Badez <al*************@gmail.comwrote:
Thanks for your try Cliff, I was very confused :P
More over I made some mistake when I post (to make it easiest).

Here is my real code:

with
dArguments = {
'argName' : {
'mandatory' : bool, # True or False
[...], # other field we do not care here
}
}

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if cls.dArguments[arg]['mandatory']:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

So, as you see, we agree each other about "if bool" or "if bool is
True" ;)

So, my question was how to give it a better 'python like' look ?
Any idea ?

For a 'python like' look lose the Hungarian notation (even Microsoft
have largely stopped using it), increase the indentation to 4 spaces,
and also get rid of the spurious parentheses around the result.
Otherwise it is fine: clear and to the point.

If you really wanted you could write something like:

m, o = [], []
for arg in cls.dArguments:
(m if cls.dArguments[arg]['mandatory'] else o).append(arg)
return m, o

Or even:
m, o = [], []
action = [o.append, m.append]
for arg in cls.dArguments:
action[bool(cls.dArguments[arg]['mandatory'])](arg)
return m, o

but it just makes the code less clear, so why bother?
Oct 24 '07 #10

P: n/a
On Oct 24, 8:02 am, kyoso...@gmail.com wrote:
On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...

You might look into list comprehensions. You could probably do this
with two of them:

<code>
# completely untested
lMandatory = [arg for arg in cls.dArguments if arg is True]
lOptional = [arg for arg in cls.dArguments if arg is False]
</code>

Something like that. I'm not the best with list comprehensions, so I
may have the syntax just slightly off. See the following links for
more information:

http://www.secnetix.de/olli/Python/l...tut/node7.html

Mike
After reading the others replies, it makes me think that I'm barking
up the wrong tree. Ah well.

Mike

Oct 24 '07 #11

P: n/a
On 2007-10-24, Alexandre Badez <al*************@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this
code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...
You can do it shorter, not sure that it also qualifies as better....

d = { True : [] , False : [] }
for arg in cls.arguments:
d[arg == True].append(arg)

return d[True], d[False]

One potential problem here is that 'arg == True' may not be the same as 'if
arg:'. I couldn't come up with a better equivalent expression, maybe one of the
other readers knows more about this?
Albert

Oct 24 '07 #12

P: n/a
Alexandre Badez wrote:
I'm just wondering, if I could write a in a "better" way this code
[...]
I think there is a better way, but I can't see how...
What's "better" for you? Shorter? More performant? More readable?
Complying with best practice? Closely following a specific
programming paradigm?

Regards,
Björn

--
BOFH excuse #403:

Sysadmin didn't hear pager go off due to loud music from bar-room
speakers.

Oct 24 '07 #13

P: n/a
On Oct 24, 2:02 pm, kyoso...@gmail.com wrote:
On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...

You might look into list comprehensions. You could probably do this
with two of them:

<code>
# completely untested
lMandatory = [arg for arg in cls.dArguments if arg is True]
lOptional = [arg for arg in cls.dArguments if arg is False]
</code>

Something like that. I'm not the best with list comprehensions, so I
may have the syntax just slightly off.
Your list comprehensions are right, but 'arg is True' and 'arg is
False' are better written as 'arg' and 'not arg' respectively.

--
Paul Hankin

Oct 24 '07 #14

P: n/a
On Oct 24, 4:15 pm, Paul Hankin <paul.han...@gmail.comwrote:
On Oct 24, 2:02 pm, kyoso...@gmail.com wrote:
On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...
You might look into list comprehensions. You could probably do this
with two of them:
<code>
# completely untested
lMandatory = [arg for arg in cls.dArguments if arg is True]
lOptional = [arg for arg in cls.dArguments if arg is False]
</code>
Something like that. I'm not the best with list comprehensions, so I
may have the syntax just slightly off.

Your list comprehensions are right, but 'arg is True' and 'arg is
False' are better written as 'arg' and 'not arg' respectively.

--
Paul Hankin
Anyone know why towards arg is True and arg is False, arg is None is
faster than arg == None ...

Oct 24 '07 #15

P: n/a
On Oct 24, 10:42 am, cokofree...@gmail.com wrote:
On Oct 24, 4:15 pm, Paul Hankin <paul.han...@gmail.comwrote:
On Oct 24, 2:02 pm, kyoso...@gmail.com wrote:
On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...
You might look into list comprehensions. You could probably do this
with two of them:
<code>
# completely untested
lMandatory = [arg for arg in cls.dArguments if arg is True]
lOptional = [arg for arg in cls.dArguments if arg is False]
</code>
Something like that. I'm not the best with list comprehensions, so I
may have the syntax just slightly off.
Your list comprehensions are right, but 'arg is True' and 'arg is
False' are better written as 'arg' and 'not arg' respectively.
--
Paul Hankin

Anyone know why towards arg is True and arg is False, arg is None is
faster than arg == None ...
And quite often incorrect, especially the "arg is True" and "arg is
False".

Oct 24 '07 #16

P: n/a
On Wed, 24 Oct 2007 16:04:28 +0200, A.T.Hofkamp wrote:
>On 2007-10-24, Alexandre Badez <al*************@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this
code

lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)

I think there is a better way, but I can't see how...

You can do it shorter, not sure that it also qualifies as better....

d = { True : [] , False : [] }
for arg in cls.arguments:
d[arg == True].append(arg)

return d[True], d[False]

One potential problem here is that 'arg == True' may not be the same as 'if
arg:'. I couldn't come up with a better equivalent expression, maybe one of the
other readers knows more about this?
With ``if arg:`` the interpreter asks `arg` for its "boolean value". So a
better way would be:

d[bool(arg)].append(arg)

As `True` and `False` are instances of `int` with the values 1 and 0 it's
possible to replace the dictionary by a list:

tmp = [[], []]
for arg in cls.arguments:
tmp[bool(arg)].append(arg)
return tmp[1], tmp[0]

Maybe that's nicer. Maybe not.

Ciao,
Marc 'BlackJack' Rintsch
Oct 24 '07 #17

P: n/a
Bjoern Schliessmann a écrit :
Alexandre Badez wrote:
>I'm just wondering, if I could write a in a "better" way this code
[...]
I think there is a better way, but I can't see how...

What's "better" for you? Shorter? More performant? More readable?
Complying with best practice? Closely following a specific
programming paradigm?
I think the OP meant "more pythonic".

Oct 24 '07 #18

P: n/a
On Oct 24, 9:04 am, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
On 2007-10-24, Alexandre Badez <alexandre.ba...@gmail.comwrote:
I'm just wondering, if I could write a in a "better" way this
code
lMandatory = []
lOptional = []
for arg in cls.dArguments:
if arg is True:
lMandatory.append(arg)
else:
lOptional.append(arg)
return (lMandatory, lOptional)
I think there is a better way, but I can't see how...

You can do it shorter, not sure that it also qualifies as better....

d = { True : [] , False : [] }
for arg in cls.arguments:
d[arg == True].append(arg)

return d[True], d[False]

One potential problem here is that 'arg == True' may not be the same as 'if
arg:'. I couldn't come up with a better equivalent expression, maybe one of the
other readers knows more about this?

Albert
d[bool(arg)].append(arg) resolves your concern?

Oct 24 '07 #19

P: n/a
On Oct 24, 3:46 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
For a 'python like' look lose the Hungarian notation (even Microsoft
have largely stopped using it)
I wish I could.
But my corporation do not want to apply python.org coding rules
increase the indentation to 4 spaces,
Well, it is in my python file.
I do not do it here, because I'm a bit lazy.
and also get rid of the spurious parentheses around the result.
Thanks
Otherwise it is fine: clear and to the point.
Thanks
>
If you really wanted you could write something like:

m, o = [], []
for arg in cls.dArguments:
(m if cls.dArguments[arg]['mandatory'] else o).append(arg)
return m, o

Or even:
m, o = [], []
action = [o.append, m.append]
for arg in cls.dArguments:
action[bool(cls.dArguments[arg]['mandatory'])](arg)
return m, o

but it just makes the code less clear, so why bother?
And finally thanks again ;)

Oct 24 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.