this script should list and sort alphabetically any CL args. it should also look for a pre-determined match variable and identify it (regardless of case).
i'm having problems with the sorting and the case part.
here's the code
#!/usr/bin/env python
import sys
if len(sys.argv) < 2:
print "No command line arguments to list"
else:
sys.argv.sort
match = 'foo'
for i in range(1,len(sys.argv)):
if sys.argv[i] == match:
print sys.argv[i], "<---MATCH"
else:
print sys.argv[i]
is my syntax/indentation right?
inkedmn
11-29-2001, 07:41 PM
it's returning the args in a list, and identifying the match variable, but only if it's in lowercase. i know i need to use foo.lower() , but i'm just not sure where...
and, the sort function isn't sorting them, they're just being printed in the order they appear on the CL.
jemfinch
11-29-2001, 10:28 PM
Originally posted by inkedmn:
#!/usr/bin/env python
import sys
if len(sys.argv) < 2:
print "No command line arguments to list"
else:
sys.argv.sort
match = 'foo'
for i in range(1,len(sys.argv)):
if sys.argv[i] == match:
print sys.argv[i], "<---MATCH"
else:
print sys.argv[i]
Indentation looks right.
Type this into your friendly python interpreter, started with some arguments on
the command line:
And you don't need to use match.lower(), that's already in lowercase. You need
instead to use sys.argv[i].lower(), which will make the argument lowercase.
Jeremy
TheLinuxDuck
11-30-2001, 10:28 AM
Originally posted by jemfinch:
<STRONG>And you don't need to use match.lower(), that's already in lowercase.</STRONG>
Just my HO, but it doesn't hurt at all to do a lower on the match variable. Simply because it will allow the coder to change the contents of the match without have to worry about case and such.. and, if the coder decides to make the match variable also be input from the user (which would be a logical next step), then having the lower on there now will only prevent having to add it later.
I know it is easy to add later, if the coder decides to do these changes, but why not do it now? It doesn't hurt anything, and it's not going to cause slowdowns in the code, simply because it's not in a loop.
Just my HO, though... (^=
YaRness
11-30-2001, 10:33 AM
Originally posted by TheLinuxDuck:
<STRONG> Just my HO, but it doesn't hurt at all to do a lower on the match variable. Simply because it will allow the coder to change the contents of the match without have to worry about case and such.. and, if the coder decides to make the match variable also be input from the user (which would be a logical next step), then having the lower on there now will only prevent having to add it later.
I know it is easy to add later, if the coder decides to do these changes, but why not do it now? It doesn't hurt anything, and it's not going to cause slowdowns in the code, simply because it's not in a loop.
Just my HO, though... (^=</STRONG>
if tld's talking about what i think he's talking about, i always do that.
my case statements always end up looking like
case uppercase(key)
case "FOO"
#do stuff
case "BAR"
#do stuff
...etc.
TheLinuxDuck
11-30-2001, 12:19 PM
YaRness:
jemfinch in pointing out that the variable <STRONG>match</STRONG>, in this case, is hardcoded to a lowercase word. He is correct in saying that it is unnecessary to use a .lower() call when comparing it, because it is already lowercase. however, my point was that it harms nothing, especially for ease in changing the contents of <STRONG>match</STRONG>, to use a .lower() on it..
But, yea, what you're doing is a good practice, I believe, since a dynamic content of a variable can be either.. (^=
I was just harassing jemfinch, mostly. (^=
YaRness
11-30-2001, 12:24 PM
Originally posted by TheLinuxDuck:
<STRONG>I was just harassing jemfinch, mostly. (^=</STRONG>always a worthy effort.
http://anubis.science.unitn.it/services/perl/perl-title.gif
inkedmn
11-30-2001, 12:40 PM
ok, i took out the match.lower line, but it's still not sorting alphabetically or lowercasing the args...
AHHHHHHHHHH!!!!!!
import sys
import string
if len(sys.argv) < 2:
print "No command line arguments to list"
else:
sys.argv.sort
match = 'foo'
for i in range(1,len(sys.argv)):
if sys.argv[i] == match:
sys.argv[i].lower()
print sys.argv[i], "<---MATCH"
else:
print sys.argv[i]
do i have the .sort and .lower functions in the right place?
Stuka
11-30-2001, 12:54 PM
Try it this way:
if sys.argv[i].lower() == match.lower()
and I think jemfinch is saying you need to do a sys.argv.sort rather than sys.argv.sort() but I'm not 100% sure.
YaRness
11-30-2001, 12:55 PM
Originally posted by inkedmn:
<STRONG>ok, i took out the match.lower line, but it's still not sorting alphabetically or lowercasing the args...
AHHHHHHHHHH!!!!!!
import sys
import string
if len(sys.argv) < 2:
print "No command line arguments to list"
else:
sys.argv.sort
match = 'foo'
for i in range(1,len(sys.argv)):
if sys.argv[i] == match:
sys.argv[i].lower()
print sys.argv[i], "<---MATCH"
else:
print sys.argv[i]
do i have the .sort and .lower functions in the right place?</STRONG>
do the lower() before you try to match it.
TheLinuxDuck
11-30-2001, 01:04 PM
[QUOTE]Originally posted by inkedmn:
sys.argv.sort
Since sort is a method member, and not a variable member of a class, it must be called with ()'s:
sys.argv.sort()
That is why it's not sorting. (^=
if sys.argv[i] == match:
sys.argv[i].lower()
does not affect the actual content of the string argv[i], it returns a new copy of the altered string. Look at this example:
Before lower: THIS IS A STRING
After lower : THIS IS A STRING
With lower : this is a string
Do you see the difference in how it is used? When you call string1.lower(), it does not actually change the data in string1, it makes a copy of string1, alters the copy, then returns the copy, so that the original is not affected.
You're doing great, though! Keep it up, dood!
TheLinuxDuck
11-30-2001, 01:31 PM
Just for clarification, I wanted to mention a little more about the sort problem.
When you call sort as:
string1.sort
python doesn't actually execute the function. Calling it like this, python will return information about the method.
The highlighted parts are the important ones.
¹ Here we have defined our method, thingie, of our class checkItYo. Don't worry about <STRONG>self</STRONG> right now, just note that the class has this particular method name.
² The first time we call our method, we call it with parenthesis. This executes the method that returns the string 'something', which is then passed into <STRONG>print</STRONG>, and displayed on the screen.
³ This time, we do not use parenthesis. But look at the output this time:
Without parens: <method checkItYo.thingie of checkItYo instance at 0x80df4ac>
Here you can see the information about the method call. It tells you the name of the method (checkItYo.thingie), the class that is resides in (checkItYo), and the memory address that the method is called from.
This is because of the way that classes work; it is a default behavior, to return a string as such.
When your code is calling <STRONG>sort</STRONG> as:
sys.argv[i].sort
It is returning a line similar to the one above, but the code isn't doing anything with the return value, so therefore, it appears that the sort is not doing anything. If you added a print before it, it would display the info string.
jemfinch
11-30-2001, 02:22 PM
Originally posted by TheLinuxDuck:
Just for clarification, I wanted to mention a little more about the sort problem.
When you call sort as:
string1.sort
python doesn't actually execute the function. Calling it like this, python will return information about the method.
Actually, that's not true. Python doesn't return information about the method,
it returns the method itself. It can still be used just like it was
anywhere else. Remember, methods/functions are first-class data objects in
Python, as much as lists, or dictionaries, or classes are.
Jeremy
inkedmn
11-30-2001, 02:30 PM
ok, here's the latest version. the sort function works now, but i can't get it to lowercase the arg that matches the 'match' variable or show "MATCH" even if it's all lower case.
and i'm losing my mind. :)
import sys
import string
if len(sys.argv) < 2:
print "No command line arguments to list"
else:
sys.argv.sort()
match = 'foo'
for i in range(1,len(sys.argv)):
if sys.argv[i].lower == match:
print sys.argv[i], "<---MATCH"
else:
print sys.argv[i]
[edit]
and whenever i put an arg on the CL that starts with 'a', it prints the name of the script (args1nc.py) instead of that argument...
[ 30 November 2001: Message edited by: inkedmn ]
YaRness
11-30-2001, 02:50 PM
<me bored at work time>
this is the code in perl, i think (i don't have an interpreter installed at the moment :( )
$match = 'foo'
foreach (sort @argv)
{
print "$_ is a match" if (lc($match) eq lc($_))
}
i'm curious if i could do
print "$_ is a match" if(lc($match) eq lc($_)) foreach (sort @argv)but it'd prolly take some extra block delimiters or something.
</>
inke have you tested that it is definitely iterating through the args like you think it is (with a print statement that goes off for each arg or something)?
TheLinuxDuck
11-30-2001, 04:23 PM
Originally posted by jemfinch:
<STRONG>Actually, that's not true. Python doesn't return information about the method,
it returns the method itself. It can still be used just like it was
anywhere else. Remember, methods/functions are first-class data objects in
Python, as much as lists, or dictionaries, or classes are.</STRONG>
My fault.. I assumed that, because it was not called specifically with paren's, it was being used in a string context, and therefore, it would return a string object. My assumption was based on the fact that, when the object (without parens) is used as:
print object.method
It returns a string object. That is why I assumed that it would also return a string object, as a default.
TheLinuxDuck
11-30-2001, 04:45 PM
now that I think about it more, it seems silly to me to think that it would be treated in a string context. I must have been drinking alot of cough syrup.
kmj
11-30-2001, 04:46 PM
TLD - To see what jemfinch means, try something like this:
def foo():
print "Blarg!"
g = foo
g()
Of course 'print' is going to turn it into a string, because that's the only thing you can print oot. :D
Stuka
11-30-2001, 05:42 PM
inked...it should be:
sys.argv[i].lower() <--notice the ()
TheLinuxDuck
11-30-2001, 06:16 PM
Originally posted by kmj:
<STRONG>TLD - To see what jemfinch means, try something like this:</STRONG>
def foo():
print "Blarg!"
g = foo
g()
I did a bunch of tests and stuff before I even replied to his post. I wanted to make sure that I had my facts right, so that I could either agree with him or disagree, but be factual. (^=
kmj
12-01-2001, 07:14 PM
Oh, don't go pulling that "factual" crap on me. :p
Btw, you must've squeezed that second response in while I was typing my reply up.
justlinux.com
Copyright Internet.com Inc. All Rights Reserved.