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;








1















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









share|improve this question



















  • 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


















1















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









share|improve this question



















  • 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














1












1








1








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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













  • 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








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













2 Answers
2






active

oldest

votes


















7














repr(thing)


will output text that when executed will (in most cases) reproduce the dictionary.






share|improve this answer























  • 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


















1














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






share|improve this answer

























    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
    );



    );













    draft saved

    draft discarded


















    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









    7














    repr(thing)


    will output text that when executed will (in most cases) reproduce the dictionary.






    share|improve this answer























    • 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















    7














    repr(thing)


    will output text that when executed will (in most cases) reproduce the dictionary.






    share|improve this answer























    • 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













    7












    7








    7







    repr(thing)


    will output text that when executed will (in most cases) reproduce the dictionary.






    share|improve this answer













    repr(thing)


    will output text that when executed will (in most cases) reproduce the dictionary.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 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
















    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













    1














    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






    share|improve this answer



























      1














      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






      share|improve this answer

























        1












        1








        1







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 10:31









        Kolay.NeKolay.Ne

        769 bronze badges




        769 bronze badges



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현