return the last item in a list

D

David Bear

I've googled for the above and get way too many hits..

I'm looking for an 'easy' way to have the last item in a list returned.

I've thought about

list[len(list)-1]

but thought there would be a more gracefull way.
 
C

Christos TZOTZIOY Georgiou

I've googled for the above and get way too many hits..

I'm looking for an 'easy' way to have the last item in a list returned.

I've thought about

list[len(list)-1]

but thought there would be a more gracefull way.

There is.

alist[-1]

Did you read the tutorial? This is referenced in "3. An Informal
Introduction to Python".
 
R

Roman Yakovenko

Hi. I have small problem. I need to load extension module that depends
on shared library. Before actually importing module I tried to edit
os.environ or to call directly to os.putenv without any success -
shared library was not found. I tried to search the Internet for the
answer. The only approach I saw was to set LD_LIBRARY_PATH before
invoking python script. I don't like this solution.

Roman
 
J

John Abel

With Solaris 8+ you would use the command crle, with Linux
(RedHat/SuSE/Mandrake) you need to add the relevant directories
/etc/ld.so.conf and run ldconfig. I've not got a Debian box to hand, so
I can't say if it matches, but that should give you a pointer.

HTH

J

Roman said:
Does it matter? If so, please explain why ( lack of knowledge )
I am using Linux ( Debian Surge )

Thanks

--
*John Abel
Senior Unix Administrator*
PA News Limited
www.pa.press.net <http://www.pa.press.net>
E-Mail address: (e-mail address removed) <mailto:[email protected]>
Telephone Number : 01430 455553
Fax Number : 0870 1240192
Mobile Number : 07971 611356
The Bishop's Manor, Market Place, Howden, DN14 7BL
PA News Limited, 292 Vauxhall Bridge Road, London SW1V 1AE. Registered
in England No. 3891053.
 
R

Roman Yakovenko

Thanks for help. But it is not exactly solution I am looking for. I
would like to do it from python script. For example

update_env() #<- this function will change LD_LIBRARY_PATH
import extension_that_depends_on_shared_library

Roman

With Solaris 8+ you would use the command crle, with Linux
(RedHat/SuSE/Mandrake) you need to add the relevant directories
/etc/ld.so.conf and run ldconfig. I've not got a Debian box to hand, so
I can't say if it matches, but that should give you a pointer.

I think I should have permissions to do it. (more over users of my
scripts should have permissions )
 
S

Serge Orlov

Roman said:
Hi. I have small problem. I need to load extension module that depends
on shared library. Before actually importing module I tried to edit
os.environ or to call directly to os.putenv without any success -
shared library was not found. I tried to search the Internet for the
answer. The only approach I saw was to set LD_LIBRARY_PATH before
invoking python script. I don't like this solution.

Looks like it's glibc linker inflexibility:
http://hathawaymix.org/Weblog/2004-12-30
"""
There is no provision for modifying the library search path once your
program has started.
"""

Python does update enviromental variables if you change os.environ or
call os.putenv, but the linker ignores the changes.

Serge.
 
J

Joal Heagney

Roman said:
Thanks for help. But it is not exactly solution I am looking for. I
would like to do it from python script. For example

update_env() #<- this function will change LD_LIBRARY_PATH
import extension_that_depends_on_shared_library

Roman




I think I should have permissions to do it. (more over users of my
scripts should have permissions )

Yep. Unfortunatly if you don't have access to the /etc/ld.so.conf file,
the only option left is your wrapper script idea. (By the way, have you
actually tested to see if setting the LD_LIBRARY_PATH actually works? If
not, you're really up the creek.)

If the script is in shell, you could use something like:

(Dotted lines denote start and end of script, not actual script content)
-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library" then
export LD_LIBRARY_PATH=$oldpath":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

This will check to see if your library path is in the LD_LIBRARY_PATH,
set it if it's not, and then run your wrapped program, passing it the
arguments that the wrapper script was called by.

Joal
 
J

Joal Heagney

Joal said:
Yep. Unfortunatly if you don't have access to the /etc/ld.so.conf file,
the only option left is your wrapper script idea. (By the way, have you
actually tested to see if setting the LD_LIBRARY_PATH actually works? If
not, you're really up the creek.)

If the script is in shell, you could use something like:

(Dotted lines denote start and end of script, not actual script content)
-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library" then
export LD_LIBRARY_PATH=$oldpath":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

This will check to see if your library path is in the LD_LIBRARY_PATH,
set it if it's not, and then run your wrapped program, passing it the
arguments that the wrapper script was called by.

Joal

Aaaarrrrggghhhh. Too long since I've programmed in script. Plus it
doesn't help changing your mind about implementation halfway through.
The script should read something like this:

-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library"
then
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

Sorry about that.

pretending-like-a-cat-that-he-didn't-just-do-something-stupid'ly yours,

Joal
 
A

Antoon Pardon

Op 2005-03-31 said:
Aaaarrrrggghhhh. Too long since I've programmed in script. Plus it
doesn't help changing your mind about implementation halfway through.
The script should read something like this:

-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library"
then
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

And you should change that last line to:

<wrapped program> "$@"
 
J

Joal Heagney

Antoon said:
And you should change that last line to:

<wrapped program> "$@"

Ah yes, because we want the arguments passed in as seperate words, not
as a whole string.

Serves me right for testing this out as:

#!/bin/sh
echo $*

:)

Joal
 
C

Christos TZOTZIOY Georgiou

Ah yes, because we want the arguments passed in as seperate words, not
as a whole string.

No, this would happen if your last line was

<wrapped program> "$*"


To summarize, suppose your script is called with the following
arguments:

<wrapper_script> "File with space.txt" arg2 arg3

Here follow "last lines" and the corresponding sys.argv[1:]:

LAST LINE: <wrapped_program> $*
SYS.ARGV : ["File", "with", "space.txt", "arg2", "arg3"]

LAST LINE: <wrapped_program> "$*"
SYS.ARGV : ["File with space.txt arg2 arg3"]

LAST LINE: <wrapped_program> "$@"
SYS.ARGV : ["File with space.txt", "arg2", "arg3"]

For more information, see man 1 bash or man 1 ksh or man 1 sh. Don't
know if this applies to *csh family.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top