Генератор на сетъри и гетъри за php (python 2.6)

Който го мързи да ги пише на ръка, си пише скрипт.

Example: python phpgetset.py par1 par2 par3

import sys
print sys.argv

def gSet(itm):
print "function set"+itm[0].capitalize() + itm[1:]+"($"+itm+")"
print "{"
print "\t$this->"+itm+" = $"+itm+";"
print "}"

def gGet(itm):
print "function get"+itm[0].capitalize() + itm[1:]+"()"
print "{"
print "\treturn $this->"+itm+";"
print "}"

for item in sys.argv[1:]:
gSet(item)
gGet(item)

След коментар от Ивайло, че това горното не е питон, пускам и неговата версия:

import sys

def GetSet(itm, get_set = True):
return "\n".join([
"function %set%s(%s)" % ("g" if get_set else "s", itm.capitalize(), "" if get_set else "$newval"),
"{",
("\treturn $this->%s;" if get_set else "\t$this->%s = $newval;") % itm.lower(),
"}", "",]) + (GetSet(itm, False) if get_set else "")

#sys.argv.append("Property")
print sys.argv

for item in sys.argv[1:]:
print GetSet(item)
This entry was posted in Други. Bookmark the permalink.

2 Responses to Генератор на сетъри и гетъри за php (python 2.6)

  1. Ivaylo says:

    Това не е Python. 🙂

    import sys

    def GetSet(get_set, itm):
    return “\n”.join([
    “function %set%s(%s)” % (“g” if get_set else “s”, itm.capitalize(), “” if get_set else “$newval”),
    “{“,
    (“\treturn $this->%s;” if get_set else “\t$this->%s = $newval;”) % itm.lower(),
    “}”,])

    print sys.argv

    for item in sys.argv[1:]:
    print GetSet(True, item)
    print GetSet(False, item)

  2. Ivaylo says:

    Всъщност това вече е Python. 🙂
    [code]import sys

    def GetSet(itm, get_set = True):
    return “\n”.join([
    “function %set%s(%s)” % (“g” if get_set else “s”, itm.capitalize(), “” if get_set else “$newval”),
    “{“,
    (“\treturn $this->%s;” if get_set else “\t$this->%s = $newval;”) % itm.lower(),
    “}”, “”,]) + (GetSet(itm, False) if get_set else “”)

    #sys.argv.append(“Property”)
    print sys.argv

    for item in sys.argv[1:]:
    print GetSet(item)
    [/code]

Leave a Reply

Your email address will not be published. Required fields are marked *