Is there something like a reverse eval() function?Calling a function of a module by using its name (a string)Why is using the JavaScript eval function a bad idea?Hidden features of PythonHow to import a module given its name?Using global variables in a functionHow to make a chain of function decorators?Reverse a string in PythonPeak detection in a 2D array“Large data” work flows using pandasError: “ 'dict' object has no attribute 'iteritems' ”
C-152 carb heat on before landing in hot weather?
What are the benefits of using the X Card safety tool in comparison to plain communication?
Distance Matrix (plugin) - QGIS
Isn't this a trivial corollary?
How well known and how commonly used was Huffman coding in 1979?
As a DM, how do you control a dysfunctional group wanting different things out of a game?
try to discover the pattern
Employer wants to use my work email account after I quit, is this legal under German law? Is this a GDPR waiver?
Do flight schools typically have dress codes or expectations?
How to split an equation over two lines?
MH370 blackbox - is it still possible to retrieve data from it?
Inverse-quotes-quine
Can the negators "jamais, rien, personne, plus, ni, aucun" be used in a single sentence?
Using “sparkling” as a diminutive of “spark” in a poem
Cascading Repair Costs following Blown Head Gasket on a 2004 Subaru Outback
How to get cool night-vision without lame drawbacks?
Why doesn't a marching band have strings?
Sho, greek letter
Is there any evidence that the small canisters (10 liters) of 95% oxygen actually help with altitude sickness?
No IMPLICIT_CONVERSION warning in this query plan
Do hotel cleaning personnel have any benefit from leaving empty bottles in the room as opposed to returning them to the store?
Does squid ink pasta bleed?
What happens when your group is victim of a surprise attack but you can't be surprised?
Is there any set of 2-6 notes that doesn't have a chord name?
Is there something like a reverse eval() function?
Calling a function of a module by using its name (a string)Why is using the JavaScript eval function a bad idea?Hidden features of PythonHow to import a module given its name?Using global variables in a functionHow to make a chain of function decorators?Reverse a string in PythonPeak detection in a 2D array“Large data” work flows using pandasError: “ 'dict' object has no attribute 'iteritems' ”
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Imagine the following problem: you have a dictionary of some content in python and want to generate python code that would create this dict. (which is like eval but in reverse)
Is there something that can do this?
Scenario:
I am working with a remote python interpreter. I can give source files to it but no input. So I am now looking for a way to encode my input data into a python source file.
Example:
d = 'a': [1,4,7]
str_d = reverse_eval(d)
# "'a': [1, 4, 7]"
eval(str_d) == d
python eval
|
show 1 more comment
Imagine the following problem: you have a dictionary of some content in python and want to generate python code that would create this dict. (which is like eval but in reverse)
Is there something that can do this?
Scenario:
I am working with a remote python interpreter. I can give source files to it but no input. So I am now looking for a way to encode my input data into a python source file.
Example:
d = 'a': [1,4,7]
str_d = reverse_eval(d)
# "'a': [1, 4, 7]"
eval(str_d) == d
python eval
2
Can you be more specific by providing an example?
– Vasilis G.
Mar 25 at 10:11
rather reverseast.literal_eval
then.
– Jean-François Fabre♦
Mar 25 at 10:20
Not quite sure why you are trying to do this. It might be an XY problem for which pickle is the solution for the unasked X problem.
– John Coleman
Mar 25 at 10:20
@JohnColeman Let's not casually throw aroundpickle
, please. We're giving newbies the impression that pickle can solve their problem, without mentioning that it opens up their program to an arbitrary code execution vulnerability. That's how we end up with people sending pickled data over an unsecured socket connection.
– Aran-Fey
Mar 25 at 10:25
@Aran-Fey Good point -- but of courseeval
has the same problems. Fortunately, the pickle documentation that I linked to has a very clear warning which is formatted so that it is the first thing you see when you look at it.
– John Coleman
Mar 25 at 10:28
|
show 1 more comment
Imagine the following problem: you have a dictionary of some content in python and want to generate python code that would create this dict. (which is like eval but in reverse)
Is there something that can do this?
Scenario:
I am working with a remote python interpreter. I can give source files to it but no input. So I am now looking for a way to encode my input data into a python source file.
Example:
d = 'a': [1,4,7]
str_d = reverse_eval(d)
# "'a': [1, 4, 7]"
eval(str_d) == d
python eval
Imagine the following problem: you have a dictionary of some content in python and want to generate python code that would create this dict. (which is like eval but in reverse)
Is there something that can do this?
Scenario:
I am working with a remote python interpreter. I can give source files to it but no input. So I am now looking for a way to encode my input data into a python source file.
Example:
d = 'a': [1,4,7]
str_d = reverse_eval(d)
# "'a': [1, 4, 7]"
eval(str_d) == d
python eval
python eval
edited May 29 at 6:26
Christian
asked Mar 25 at 10:07
ChristianChristian
3771 gold badge6 silver badges19 bronze badges
3771 gold badge6 silver badges19 bronze badges
2
Can you be more specific by providing an example?
– Vasilis G.
Mar 25 at 10:11
rather reverseast.literal_eval
then.
– Jean-François Fabre♦
Mar 25 at 10:20
Not quite sure why you are trying to do this. It might be an XY problem for which pickle is the solution for the unasked X problem.
– John Coleman
Mar 25 at 10:20
@JohnColeman Let's not casually throw aroundpickle
, please. We're giving newbies the impression that pickle can solve their problem, without mentioning that it opens up their program to an arbitrary code execution vulnerability. That's how we end up with people sending pickled data over an unsecured socket connection.
– Aran-Fey
Mar 25 at 10:25
@Aran-Fey Good point -- but of courseeval
has the same problems. Fortunately, the pickle documentation that I linked to has a very clear warning which is formatted so that it is the first thing you see when you look at it.
– John Coleman
Mar 25 at 10:28
|
show 1 more comment
2
Can you be more specific by providing an example?
– Vasilis G.
Mar 25 at 10:11
rather reverseast.literal_eval
then.
– Jean-François Fabre♦
Mar 25 at 10:20
Not quite sure why you are trying to do this. It might be an XY problem for which pickle is the solution for the unasked X problem.
– John Coleman
Mar 25 at 10:20
@JohnColeman Let's not casually throw aroundpickle
, please. We're giving newbies the impression that pickle can solve their problem, without mentioning that it opens up their program to an arbitrary code execution vulnerability. That's how we end up with people sending pickled data over an unsecured socket connection.
– Aran-Fey
Mar 25 at 10:25
@Aran-Fey Good point -- but of courseeval
has the same problems. Fortunately, the pickle documentation that I linked to has a very clear warning which is formatted so that it is the first thing you see when you look at it.
– John Coleman
Mar 25 at 10:28
2
2
Can you be more specific by providing an example?
– Vasilis G.
Mar 25 at 10:11
Can you be more specific by providing an example?
– Vasilis G.
Mar 25 at 10:11
rather reverse
ast.literal_eval
then.– Jean-François Fabre♦
Mar 25 at 10:20
rather reverse
ast.literal_eval
then.– Jean-François Fabre♦
Mar 25 at 10:20
Not quite sure why you are trying to do this. It might be an XY problem for which pickle is the solution for the unasked X problem.
– John Coleman
Mar 25 at 10:20
Not quite sure why you are trying to do this. It might be an XY problem for which pickle is the solution for the unasked X problem.
– John Coleman
Mar 25 at 10:20
@JohnColeman Let's not casually throw around
pickle
, please. We're giving newbies the impression that pickle can solve their problem, without mentioning that it opens up their program to an arbitrary code execution vulnerability. That's how we end up with people sending pickled data over an unsecured socket connection.– Aran-Fey
Mar 25 at 10:25
@JohnColeman Let's not casually throw around
pickle
, please. We're giving newbies the impression that pickle can solve their problem, without mentioning that it opens up their program to an arbitrary code execution vulnerability. That's how we end up with people sending pickled data over an unsecured socket connection.– Aran-Fey
Mar 25 at 10:25
@Aran-Fey Good point -- but of course
eval
has the same problems. Fortunately, the pickle documentation that I linked to has a very clear warning which is formatted so that it is the first thing you see when you look at it.– John Coleman
Mar 25 at 10:28
@Aran-Fey Good point -- but of course
eval
has the same problems. Fortunately, the pickle documentation that I linked to has a very clear warning which is formatted so that it is the first thing you see when you look at it.– John Coleman
Mar 25 at 10:28
|
show 1 more comment
2 Answers
2
active
oldest
votes
repr(thing)
will output text that when executed will (in most cases) reproduce the dictionary.
To elaborate: the purpose ofrepr
is to return a string which, if possible, will reconstruct an equivalent object when passed toeval
. That's why the Python REPL usesrepr
when printing objects.
– Daniel Pryden
Mar 25 at 10:13
add a comment |
Actually, it's important for which types of data do you want this reverse function to exist. If you're talking about built-in/standard classes, usually their .__repr__()
method returns the code you want to access. But if your goal is to save something in a human-readable format, but to use an eval-like function to use this data in python, there is a json
library.
It's better to use json for this reason because using eval is not safe.
Json's problem is that it can't save any type of data, it can save only standard objects, but if we're talking about not built-in types of data, you never know, what is at their .__repr__()
, so there's no way to use repr-eval with this kind of data
So, there is no reverse function for all types of data, you can use repr-eval for built-in, but for built-in data the json
library is better at least because it's safe
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%2f55335355%2fis-there-something-like-a-reverse-eval-function%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
repr(thing)
will output text that when executed will (in most cases) reproduce the dictionary.
To elaborate: the purpose ofrepr
is to return a string which, if possible, will reconstruct an equivalent object when passed toeval
. That's why the Python REPL usesrepr
when printing objects.
– Daniel Pryden
Mar 25 at 10:13
add a comment |
repr(thing)
will output text that when executed will (in most cases) reproduce the dictionary.
To elaborate: the purpose ofrepr
is to return a string which, if possible, will reconstruct an equivalent object when passed toeval
. That's why the Python REPL usesrepr
when printing objects.
– Daniel Pryden
Mar 25 at 10:13
add a comment |
repr(thing)
will output text that when executed will (in most cases) reproduce the dictionary.
repr(thing)
will output text that when executed will (in most cases) reproduce the dictionary.
answered Mar 25 at 10:12
David JonesDavid Jones
2,21619 silver badges32 bronze badges
2,21619 silver badges32 bronze badges
To elaborate: the purpose ofrepr
is to return a string which, if possible, will reconstruct an equivalent object when passed toeval
. That's why the Python REPL usesrepr
when printing objects.
– Daniel Pryden
Mar 25 at 10:13
add a comment |
To elaborate: the purpose ofrepr
is to return a string which, if possible, will reconstruct an equivalent object when passed toeval
. That's why the Python REPL usesrepr
when printing objects.
– Daniel Pryden
Mar 25 at 10:13
To elaborate: the purpose of
repr
is to return a string which, if possible, will reconstruct an equivalent object when passed to eval
. That's why the Python REPL uses repr
when printing objects.– Daniel Pryden
Mar 25 at 10:13
To elaborate: the purpose of
repr
is to return a string which, if possible, will reconstruct an equivalent object when passed to eval
. That's why the Python REPL uses repr
when printing objects.– Daniel Pryden
Mar 25 at 10:13
add a comment |
Actually, it's important for which types of data do you want this reverse function to exist. If you're talking about built-in/standard classes, usually their .__repr__()
method returns the code you want to access. But if your goal is to save something in a human-readable format, but to use an eval-like function to use this data in python, there is a json
library.
It's better to use json for this reason because using eval is not safe.
Json's problem is that it can't save any type of data, it can save only standard objects, but if we're talking about not built-in types of data, you never know, what is at their .__repr__()
, so there's no way to use repr-eval with this kind of data
So, there is no reverse function for all types of data, you can use repr-eval for built-in, but for built-in data the json
library is better at least because it's safe
add a comment |
Actually, it's important for which types of data do you want this reverse function to exist. If you're talking about built-in/standard classes, usually their .__repr__()
method returns the code you want to access. But if your goal is to save something in a human-readable format, but to use an eval-like function to use this data in python, there is a json
library.
It's better to use json for this reason because using eval is not safe.
Json's problem is that it can't save any type of data, it can save only standard objects, but if we're talking about not built-in types of data, you never know, what is at their .__repr__()
, so there's no way to use repr-eval with this kind of data
So, there is no reverse function for all types of data, you can use repr-eval for built-in, but for built-in data the json
library is better at least because it's safe
add a comment |
Actually, it's important for which types of data do you want this reverse function to exist. If you're talking about built-in/standard classes, usually their .__repr__()
method returns the code you want to access. But if your goal is to save something in a human-readable format, but to use an eval-like function to use this data in python, there is a json
library.
It's better to use json for this reason because using eval is not safe.
Json's problem is that it can't save any type of data, it can save only standard objects, but if we're talking about not built-in types of data, you never know, what is at their .__repr__()
, so there's no way to use repr-eval with this kind of data
So, there is no reverse function for all types of data, you can use repr-eval for built-in, but for built-in data the json
library is better at least because it's safe
Actually, it's important for which types of data do you want this reverse function to exist. If you're talking about built-in/standard classes, usually their .__repr__()
method returns the code you want to access. But if your goal is to save something in a human-readable format, but to use an eval-like function to use this data in python, there is a json
library.
It's better to use json for this reason because using eval is not safe.
Json's problem is that it can't save any type of data, it can save only standard objects, but if we're talking about not built-in types of data, you never know, what is at their .__repr__()
, so there's no way to use repr-eval with this kind of data
So, there is no reverse function for all types of data, you can use repr-eval for built-in, but for built-in data the json
library is better at least because it's safe
answered Mar 25 at 10:31
Kolay.NeKolay.Ne
769 bronze badges
769 bronze badges
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%2f55335355%2fis-there-something-like-a-reverse-eval-function%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
2
Can you be more specific by providing an example?
– Vasilis G.
Mar 25 at 10:11
rather reverse
ast.literal_eval
then.– Jean-François Fabre♦
Mar 25 at 10:20
Not quite sure why you are trying to do this. It might be an XY problem for which pickle is the solution for the unasked X problem.
– John Coleman
Mar 25 at 10:20
@JohnColeman Let's not casually throw around
pickle
, please. We're giving newbies the impression that pickle can solve their problem, without mentioning that it opens up their program to an arbitrary code execution vulnerability. That's how we end up with people sending pickled data over an unsecured socket connection.– Aran-Fey
Mar 25 at 10:25
@Aran-Fey Good point -- but of course
eval
has the same problems. Fortunately, the pickle documentation that I linked to has a very clear warning which is formatted so that it is the first thing you see when you look at it.– John Coleman
Mar 25 at 10:28