473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Converting a strng to an anonymous function

I'm writing a program to sort files with arbitrary python code. The
method I'm using for that is to pass sort an anonymous function taken
from the arguments. I'm wondering how to change a raw string into an
anonyous function.
Sep 29 '08 #1
2 1008
On Sep 29, 11:25 pm, Nathan Seese <uninver...@lavabit.comwrote:
I'm writing a program to sort files with arbitrary python code. The
method I'm using for that is to pass sort an anonymous function taken
from the arguments. I'm wondering how to change a raw string into an
anonyous function.
Is this enough for you?
>>L = [1, -5, 7, -9]
sorted(L, key=abs)
[1, -5, 7, -9]
>>func = "abs"
sorted(L, key=eval(func))
[1, -5, 7, -9]
>>func = "abs(x)"
sorted(L, key=lambda x: eval(func))
[1, -5, 7, -9]

Bye,
bearophile
Sep 29 '08 #2
On Sep 29, 11:25 pm, Nathan Seese <uninver...@lavabit.comwrote:
>I'm writing a program to sort files with arbitrary python code. The
method I'm using for that is to pass sort an anonymous function taken
from the arguments. I'm wondering how to change a raw string into an
anonyous function.

Is this enough for you?
>>>L = [1, -5, 7, -9]
sorted(L, key=abs)
[1, -5, 7, -9]
>>>func = "abs"
sorted(L, key=eval(func))
[1, -5, 7, -9]
>>>func = "abs(x)"
sorted(L, key=lambda x: eval(func))
[1, -5, 7, -9]

Bye,
bearophile
Thanks, that works great!
Sep 29 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Peter Wilkinson | last post by:
Hello tlistmembers, I am using the encoding function to convert unicode to ascii. At one point this code was working just fine, however, now it has broken. I am reading a text file that has is...
76
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax,...
6
by: DigitalRick | last post by:
I have been running CDONTS in my ASPpages to send emails to me sent from my guestbook. It had been working fine untill I upgraded to Server 2003 (I am also running Exchange 2003) all locally. I...
7
by: Paul K | last post by:
I'm writing a small component that needs to be as fast as possible. The component needs to convert a string to decimal during the course of it's processing. However, I need to test the string...
2
by: Bonj | last post by:
I have got the starting bits of a game that I am trying to write. I did start to write it in VC++6, but found the basic windows GDI to be of general poor performance for animation. So I decided to...
12
by: Rob C | last post by:
Are there any good papers/documentation around for taking an unmanaged VC++ Project using MFC (created in VC V6) and making it managed? My main goal here to be able to implement new functionality...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
60
by: jacob navia | last post by:
Gnu C features some interesting extensions, among others compound statements that return a value. For instance: ({ int y = foo(); int z; if (y>0) z = y; else z=-y; z; }) A block enclosed by...
3
by: Dhananjay | last post by:
Hi All, I am facing problem when i am converting C#.net code(Delegate concept) into vb.net. I am unable to do that . Can someone help me to solve the problem. I am providing my C#.net code. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.