converting a string to a function parameter

alex goretoy aleksandr.goretoy at gmail.com
Sat Mar 14 16:45:52 CET 2009


My new class I've been working on might help you. It does what you are
asking without eval (I'm still working on it) Anyone have any ideas aswell
along with the OP, thank you

#!/usr/bin env python
#
# -*- coding: UTF-8 -*-
#
# PyNutButter BETA Version 0.1.0.1
#
# Copyright 2009 - Infinity by Alex Goretoy, All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of Vinay Sajip
# not be used in advertising or publicity pertaining to distribution
# of the software without specific, written prior permission.
# VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
OR
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER
# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# SEE ALSO LICENSE FILE IN PROJECT ROOT DIRECTORY
#
__author__  = "Aleksandr Ilyich Goretoy <agoretoy at gmail.com>"
__status__  = "beta"
__version__ = "0.1.0.1"
__date__    = "14 March 2009"

import sys, os


from colors import colors as _ck
from has_colors import _has_colors as _ha
from logg import _brush as _ers

class colors(object):
    def
__init__(self,colors_active=1,output_caller=1,caller_color="red",default=1,
                    show_lineno_write=1,show_lineno_caller=1,break_all=1,
                    logging=1,log_type="INFO",depth=10):
        """
        stdout colorization class - prints messages in color to stdout

        colors_active - output to stdout in color? 1 or 0
        output_caller - print output when control enters this
class/functions ,output caller and called function
        caller_color - color to use to output caller only if output_caller
is 1
        """

        self.caller_color = caller_color #stdout_colors function caller
called output color
        self.output_caller = output_caller # output caller and called
function
        self.colors_active = colors_active #active colors for output

        self.has_colors = _ha(sys.stdout)
        self.colors = _ck
        crack=_ers(logger="simpleExample",level="INFO",

log_file=log_file=os.environ["PWD"]+"/logs/eggo",spc=1,brk=1,slk=1,fn=1)

        #Thanks Gabriel Genellina, no crack was smoked while making crack
like this
        #list( ( self.__setattr__(x.replace("b_",""),getattr(B,x))  for x in
dir(B) if x.startswith("b_") ) )
        for smoke in dir(crack):
            if smoke.startswith("b_"):
                setattr(self, smoke[2:], getattr(crack, smoke))

        """
        these colors may not be correct: depending on how you have your
terminal configured
        """

        #if self.output_caller:
            #self.me_him(['ENTER COLORS',__name__],self.caller_color)


        """
        color and value
        """
        self.color="default"#display color
        self.value=""#used to store displayed message


    def w(self,value,color="red",level="INFO"):
        self.write(value,color,level)
    def write(self,value,color="red",level="INFO"):
        """
        write - output message,take value string or list,color=""
        """
        self._hero(value,color,level)


    def h(self,color="red",level="DEBUG"):
        self.him(color,level)
    def him(self,color="red",level="DEBUG"):
        """
        him - determines caller function name, takes color=""
        """

        if self.output_caller:

value=sys._getframe(2).f_code.co_name#+inspect.currentframe().f_back.f_lineno
            self._hero(value,color,level)

    def m_h(self,value,color="purple",level="DEBUG"):
        self.me_him(value,color,level)
    def me_him(self,value,color="purple",log_type="DEBUG"):
        """
        me_him - determines current function prepends class name and
displays caller function
        """
        if self.output_caller:



value="".join(value)+"."+sys._getframe(1).f_code.co_name+self.colors['default']+sys._getframe(2).f_code.co_name+"\x1b[00m"

            self._hero(value,color,level)

    def m(self,value,color="blue",level="DEBUG"):
        self.me(value,color,level)
    def me(self,value,color="blue",level="DEBUG"):
        """
        me - determines current function prepends class name, takes
value=__name__,color=""
        """
#        self.frames()
        value="".join(str(value)) + "." +
"".join(sys._getframe(1).f_code.co_name)
        self._hero(value,color,level)

    def _hero(self,value,color,level="INFO"):
        if self.colors_active:
            try:
                if self.colors[color] and self.has_colors != None:
                    self.reaper(value,self.colors[color],level)
                    self.spacer()
                else:
                    self.reaper(value,self.colors["default"],level)
                    self.spacer()
            except (KeyError):
                self.reaper(value,self.colors['default'],"ERROR")
                self.space()
        else:
            self.grow(self.log(value,level))
-Alex Goretoy
http://www.goretoy.com



On Fri, Mar 13, 2009 at 4:45 PM, Aaron Brady <castironpi at gmail.com> wrote:

> On Mar 13, 3:21 pm, Paul McGuire <pt... at austin.rr.com> wrote:
> > On Mar 13, 11:46 am, Aaron Brady <castiro... at gmail.com> wrote:
> >
> >
> >
> > > On Mar 13, 2:52 am, koranthala <koranth... at gmail.com> wrote:
> >
> > > > Hi,
> > > >     Is it possible to convert a string to a function parameter?
> > > > Ex:
> > > > str = 'True, type=rect, sizes=[3, 4]'
> > > > and I should be able to use it as:
> > > > test(convert(str)) and the behaviour should be same as calling test
> > > > with those values :
> > > > i.e. test(True, type=rect, sizes=[3, 4])
> >
> > > > I tried eval, but it did not work. And any other mechanism I think
> > > > turns out to be creating a full fledged python parser.
> >
> > > > Is there any mechanism with which we can do this straight away?
> >
> > > I heard 'pyparsing' was good.  ...Not that I've even been to its
> > > webpage.
> >
> > Did someone say 'pyparsing'? :)  Here is a first cut (partially lifted
> > from a previous post):
> snip 40 lines
> > Prints:
> >
> > Args: [True]
> > Kwargs: {'coords': ([1, 2], [3, 4]), 'type': 'rect', 'sizes': [3, 4]}
>
> Ha, ok, out of my league.  It's a bit heavyweight I accede.  The OP
> didn't say what s/he knew about his/er data prior, what fault
> tolerance s/he needed, what complexity and nesting of data in the
> string, etc.
>
> Hmmm..., just thinking.  Could the strings come from a python file:
> test1= fargs(True, type=rect, sizes=[3, 4])
> test2= fargs(...)
> ?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090314/e46204f1/attachment.html>


More information about the Python-list mailing list