Hi,
I am working on a small project as well as trying to learn python. I parse a
text file like
def some_user_func():
# <name> Some user function
#<description> The description
pass
so that 'name', 'description' get put into self.info and the code gets
compilied into a runnable function in self.func. This works but is it
pythonic?
Thanks alot for helping out! matthew.
def print_message():
print "I am a message"
def print_num(num=0):
print 'Number: ', num
class call_me(object):
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __call__(self, *args, **kw):
print "Execing..."
return self.func(*self.args, **self.kw)
class a_func(object):
def __init__(self, func, info = {}): # name, description, authour, etc
self.func = func
self.info = info
print 'finished a_func.__init__()'
def __call__(self):
print self.info
self.func()
print 'exiting a_func.__call__()'
my_func = a_func(call_me(print_message),{'name': 'Print a message'})
my_func()
another = a_func(call_me(print_num, 42), {'name': 'Print a number'})
another() 4 1512
Sorry, I should add that the code fragment below is just trying to work out
howto construct the python object to hold the runnable and its information.
To test I fudged what would be the results of parsing in 'my_func' and
'another' in __main__.
----- Original Message -----
From: "Matthew" <ma*****@newsgroups.com>
Newsgroups: comp.lang.python
Sent: Thursday, November 06, 2003 8:47 AM
Subject: Is this horrible python code?
"Matthew" <ma*****@newsgroups.com> wrote in message
news:bo**********@lust.ihug.co.nz... Hi,
I am working on a small project as well as trying to learn python. I parse
a text file like
def some_user_func(): # <name> Some user function #<description> The description pass
so that 'name', 'description' get put into self.info and the code gets compilied into a runnable function in self.func. This works but is it pythonic? Thanks alot for helping out! matthew.
def print_message(): print "I am a message"
def print_num(num=0): print 'Number: ', num
class call_me(object): def __init__(self, func, *args, **kw): self.func = func self.args = args self.kw = kw def __call__(self, *args, **kw): print "Execing..." return self.func(*self.args, **self.kw)
class a_func(object): def __init__(self, func, info = {}): # name, description, authour, etc self.func = func self.info = info print 'finished a_func.__init__()' def __call__(self): print self.info self.func() print 'exiting a_func.__call__()'
my_func = a_func(call_me(print_message),{'name': 'Print a message'}) my_func() another = a_func(call_me(print_num, 42), {'name': 'Print a number'}) another()
I'd be happy to help except I cannot decipher what you are asking.
Chris
"Matthew" <ma*****@newsgroups.com> wrote in message news:<bo**********@lust.ihug.co.nz>... Sorry, I should add that the code fragment below is just trying to work out howto construct the python object to hold the runnable and its information. To test I fudged what would be the results of parsing in 'my_func' and 'another' in __main__. ----- Original Message ----- From: "Matthew" <ma*****@newsgroups.com> Newsgroups: comp.lang.python Sent: Thursday, November 06, 2003 8:47 AM Subject: Is this horrible python code? "Matthew" <ma*****@newsgroups.com> wrote in message news:bo**********@lust.ihug.co.nz... Hi,
I am working on a small project as well as trying to learn python. I parse a text file like
def some_user_func(): # <name> Some user function #<description> The description pass
so that 'name', 'description' get put into self.info and the code gets compilied into a runnable function in self.func. This works but is it pythonic? Thanks alot for helping out! matthew.
def print_message(): print "I am a message"
def print_num(num=0): print 'Number: ', num
class call_me(object): def __init__(self, func, *args, **kw): self.func = func self.args = args self.kw = kw def __call__(self, *args, **kw): print "Execing..." return self.func(*self.args, **self.kw)
class a_func(object): def __init__(self, func, info = {}): # name, description, authour, etc self.func = func self.info = info print 'finished a_func.__init__()' def __call__(self): print self.info self.func() print 'exiting a_func.__call__()'
my_func = a_func(call_me(print_message),{'name': 'Print a message'}) my_func() another = a_func(call_me(print_num, 42), {'name': 'Print a number'}) another()
Matthew <ma*****@newsgroups.com> wrote: Hi,
class call_me(object): def __init__(self, func, *args, **kw): self.func = func self.args = args self.kw = kw def __call__(self, *args, **kw): print "Execing..." return self.func(*self.args, **self.kw)
I'm not sure what you're trying to do with the rest of this, but
I think that your call_me class isn't doing what you want.
asteroids% python
Python 2.2 (#1, Feb 18 2002, 14:48:51) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information. class A(object):
.... def __init__(self,a):
.... self.a = a
.... def __call__(self,a):
.... print "self.a is '%s' and a is '%s'" % (self.a,a)
.... a = A('the first thing') a('the second thing')
self.a is 'the first thing' and a is 'the second thing'
so, i don't think your __call__ method will do what you
want. then again, i'm not exactly sure what you're trying to
do, so i can't figure out why you want to initialize things
your objects with func, args and kw *and* want to be able
to specify args and kw at call time.
-michael
I think it is simpler using closures.
def auditFunc(info, func, *args, **kw):
def inner():
print 'finished a_func.__init__()'
print info
print "Execing..."
ret = func(*args, **kw)
print 'exiting a_func.__call__()'
return ret
return inner
f = auditFunc({'name': 'Print a message'}, print_message)
f()
another = auditFunc({'name': 'Print a number'}, print_num, 42)
another() This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stephen Thorne |
last post by:
Hi,
import re
foo_pattern = re.compile('foo')
'>>> m = foo_pattern.search(subject)
'>>> if m:
'>>> pass
'>>> else:
'>>> pass
|
by: Oliver Brausch |
last post by:
Hello,
have you ever heard about this MS-visual c compiler bug?
look at the small prog:
static int x=0;
int bit32() {
return ++x;
}
|
by: andrew lowe |
last post by:
Hi there,
We have to use a com interop library generated by tlbimp, where the orginal
com object is written in uniface (a compuware language). The method we use
and have the problem with has the...
|
by: Ben Taylor |
last post by:
I've asked about includes before in this group in order to
stop files being #included either twice or not at all, and
somebody said to use extern keyword for global variables
that need to be used...
|
by: Hakuin |
last post by:
Hello,
I installed visual web developer 2005 express, and started to use it
with mysql/odbc driver. It happens that when I open the database pane,
and make some operations with the sql manager,...
|
by: JonathanOrlev |
last post by:
Hello everybody,
I wrote this comment in another message of mine, but decided to post it
again as a standalone message.
I think that Microsoft's Office 2003 help system is horrible, probably...
|
by: ilikewine |
last post by:
The menu drop down icons (only the gray inactive icons) look horrible
in Windows Classic Theme. Once they are active and have color, they
look fine. If we switch to XP theme, they look just fine....
|
by: tenxian |
last post by:
Could you put up with the horrible PHP code?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |