how to pass the function name to be used in python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
i have the following json template
template.py
from string import Template
test1 = Template(u'''
{
"data": {
"name": "$name"
}
}
''')
and to generate the JSONs I use
JSONGen.py
import template
class JSONGen:
result1 = template.test1.safe_substitute(
name = 'SomeName'
)
print(result1)
now that works, it produces the JSON but i'm trying to create a function that accepts the template name and calls it something like
JSONGenV2.py
import template
class JSONGenV2:
def template_func(self, templateName):
generatedTemplate = template.templateName.safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
template_func(test1)
now what i want to achieve is to use 'templateName' contents to be the template to call, as it is right now
template.templateName.safe_substitute
gives me an error saying 'templateName' doesn't exist, how can 'templateName' be a changed to the value passed in tihs case 'test1' so it would call
template.test1.safe_substitute
Thank you
python json python-2.x
add a comment |
i have the following json template
template.py
from string import Template
test1 = Template(u'''
{
"data": {
"name": "$name"
}
}
''')
and to generate the JSONs I use
JSONGen.py
import template
class JSONGen:
result1 = template.test1.safe_substitute(
name = 'SomeName'
)
print(result1)
now that works, it produces the JSON but i'm trying to create a function that accepts the template name and calls it something like
JSONGenV2.py
import template
class JSONGenV2:
def template_func(self, templateName):
generatedTemplate = template.templateName.safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
template_func(test1)
now what i want to achieve is to use 'templateName' contents to be the template to call, as it is right now
template.templateName.safe_substitute
gives me an error saying 'templateName' doesn't exist, how can 'templateName' be a changed to the value passed in tihs case 'test1' so it would call
template.test1.safe_substitute
Thank you
python json python-2.x
You have to rethink your question title, so others can find your question better
– user8408080
Nov 16 '18 at 19:00
1
That is not a safe way to generate JSON. The "safe" in the method name doesn't mean that.
– user2357112
Nov 16 '18 at 19:05
add a comment |
i have the following json template
template.py
from string import Template
test1 = Template(u'''
{
"data": {
"name": "$name"
}
}
''')
and to generate the JSONs I use
JSONGen.py
import template
class JSONGen:
result1 = template.test1.safe_substitute(
name = 'SomeName'
)
print(result1)
now that works, it produces the JSON but i'm trying to create a function that accepts the template name and calls it something like
JSONGenV2.py
import template
class JSONGenV2:
def template_func(self, templateName):
generatedTemplate = template.templateName.safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
template_func(test1)
now what i want to achieve is to use 'templateName' contents to be the template to call, as it is right now
template.templateName.safe_substitute
gives me an error saying 'templateName' doesn't exist, how can 'templateName' be a changed to the value passed in tihs case 'test1' so it would call
template.test1.safe_substitute
Thank you
python json python-2.x
i have the following json template
template.py
from string import Template
test1 = Template(u'''
{
"data": {
"name": "$name"
}
}
''')
and to generate the JSONs I use
JSONGen.py
import template
class JSONGen:
result1 = template.test1.safe_substitute(
name = 'SomeName'
)
print(result1)
now that works, it produces the JSON but i'm trying to create a function that accepts the template name and calls it something like
JSONGenV2.py
import template
class JSONGenV2:
def template_func(self, templateName):
generatedTemplate = template.templateName.safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
template_func(test1)
now what i want to achieve is to use 'templateName' contents to be the template to call, as it is right now
template.templateName.safe_substitute
gives me an error saying 'templateName' doesn't exist, how can 'templateName' be a changed to the value passed in tihs case 'test1' so it would call
template.test1.safe_substitute
Thank you
python json python-2.x
python json python-2.x
edited Nov 16 '18 at 19:33
Alex_P
3291415
3291415
asked Nov 16 '18 at 18:37
efxefx
185
185
You have to rethink your question title, so others can find your question better
– user8408080
Nov 16 '18 at 19:00
1
That is not a safe way to generate JSON. The "safe" in the method name doesn't mean that.
– user2357112
Nov 16 '18 at 19:05
add a comment |
You have to rethink your question title, so others can find your question better
– user8408080
Nov 16 '18 at 19:00
1
That is not a safe way to generate JSON. The "safe" in the method name doesn't mean that.
– user2357112
Nov 16 '18 at 19:05
You have to rethink your question title, so others can find your question better
– user8408080
Nov 16 '18 at 19:00
You have to rethink your question title, so others can find your question better
– user8408080
Nov 16 '18 at 19:00
1
1
That is not a safe way to generate JSON. The "safe" in the method name doesn't mean that.
– user2357112
Nov 16 '18 at 19:05
That is not a safe way to generate JSON. The "safe" in the method name doesn't mean that.
– user2357112
Nov 16 '18 at 19:05
add a comment |
2 Answers
2
active
oldest
votes
Use getattr()
, usage like this:
getattr
(object, name[, default])
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example,
getattr(x, 'foobar')
is equivalent tox.foobar
. If the named attribute does not exist, default is returned if provided, otherwiseAttributeError
is raised.
Applied to your code:
class JSONGenV2:
def template_func(self, templateName):
generatedTemplate = getattr(template, templateName).safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
template_func(test1)
thank you @idlehands this is exactly what i was looking for
– efx
Nov 16 '18 at 20:28
add a comment |
You need a way to convert a template name to instance of the actual template.
I would define a dictionary with the keys being the template name, and value being the template instance.
test1 = Template(...)
test2 = Template(...)
templates = {
'test1': test1
'test2': test2
}
Now in your method you could use the templates dictionary to get the instance of the template you requested:
def template_func(self, templateName):
generatedTemplate = templates[templateName].safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
And you would call the method like so: template_func('test1')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343594%2fhow-to-pass-the-function-name-to-be-used-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use getattr()
, usage like this:
getattr
(object, name[, default])
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example,
getattr(x, 'foobar')
is equivalent tox.foobar
. If the named attribute does not exist, default is returned if provided, otherwiseAttributeError
is raised.
Applied to your code:
class JSONGenV2:
def template_func(self, templateName):
generatedTemplate = getattr(template, templateName).safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
template_func(test1)
thank you @idlehands this is exactly what i was looking for
– efx
Nov 16 '18 at 20:28
add a comment |
Use getattr()
, usage like this:
getattr
(object, name[, default])
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example,
getattr(x, 'foobar')
is equivalent tox.foobar
. If the named attribute does not exist, default is returned if provided, otherwiseAttributeError
is raised.
Applied to your code:
class JSONGenV2:
def template_func(self, templateName):
generatedTemplate = getattr(template, templateName).safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
template_func(test1)
thank you @idlehands this is exactly what i was looking for
– efx
Nov 16 '18 at 20:28
add a comment |
Use getattr()
, usage like this:
getattr
(object, name[, default])
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example,
getattr(x, 'foobar')
is equivalent tox.foobar
. If the named attribute does not exist, default is returned if provided, otherwiseAttributeError
is raised.
Applied to your code:
class JSONGenV2:
def template_func(self, templateName):
generatedTemplate = getattr(template, templateName).safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
template_func(test1)
Use getattr()
, usage like this:
getattr
(object, name[, default])
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example,
getattr(x, 'foobar')
is equivalent tox.foobar
. If the named attribute does not exist, default is returned if provided, otherwiseAttributeError
is raised.
Applied to your code:
class JSONGenV2:
def template_func(self, templateName):
generatedTemplate = getattr(template, templateName).safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
template_func(test1)
answered Nov 16 '18 at 18:56
IdlehandsIdlehands
6,1631923
6,1631923
thank you @idlehands this is exactly what i was looking for
– efx
Nov 16 '18 at 20:28
add a comment |
thank you @idlehands this is exactly what i was looking for
– efx
Nov 16 '18 at 20:28
thank you @idlehands this is exactly what i was looking for
– efx
Nov 16 '18 at 20:28
thank you @idlehands this is exactly what i was looking for
– efx
Nov 16 '18 at 20:28
add a comment |
You need a way to convert a template name to instance of the actual template.
I would define a dictionary with the keys being the template name, and value being the template instance.
test1 = Template(...)
test2 = Template(...)
templates = {
'test1': test1
'test2': test2
}
Now in your method you could use the templates dictionary to get the instance of the template you requested:
def template_func(self, templateName):
generatedTemplate = templates[templateName].safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
And you would call the method like so: template_func('test1')
add a comment |
You need a way to convert a template name to instance of the actual template.
I would define a dictionary with the keys being the template name, and value being the template instance.
test1 = Template(...)
test2 = Template(...)
templates = {
'test1': test1
'test2': test2
}
Now in your method you could use the templates dictionary to get the instance of the template you requested:
def template_func(self, templateName):
generatedTemplate = templates[templateName].safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
And you would call the method like so: template_func('test1')
add a comment |
You need a way to convert a template name to instance of the actual template.
I would define a dictionary with the keys being the template name, and value being the template instance.
test1 = Template(...)
test2 = Template(...)
templates = {
'test1': test1
'test2': test2
}
Now in your method you could use the templates dictionary to get the instance of the template you requested:
def template_func(self, templateName):
generatedTemplate = templates[templateName].safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
And you would call the method like so: template_func('test1')
You need a way to convert a template name to instance of the actual template.
I would define a dictionary with the keys being the template name, and value being the template instance.
test1 = Template(...)
test2 = Template(...)
templates = {
'test1': test1
'test2': test2
}
Now in your method you could use the templates dictionary to get the instance of the template you requested:
def template_func(self, templateName):
generatedTemplate = templates[templateName].safe_substitute(
name = 'SomeName'
)
print (generatedTemplate)
And you would call the method like so: template_func('test1')
answered Nov 16 '18 at 18:47
David BarishevDavid Barishev
319319
319319
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343594%2fhow-to-pass-the-function-name-to-be-used-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You have to rethink your question title, so others can find your question better
– user8408080
Nov 16 '18 at 19:00
1
That is not a safe way to generate JSON. The "safe" in the method name doesn't mean that.
– user2357112
Nov 16 '18 at 19:05