Save data in Python and then read in C++ [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have some code in Python which generates a set of data structures (they may be represented by classes but no methods are needed).
This data structures may be extended or added new structures in future
I have some code in C++ on Android which knew about part of this data structures and their fields.
The only way to pass data structures is through serialization to file and then deserialize them
Binary format support is needed.
Mature implementations in Python and C++ are needed. BSD, MIT, Apache licenses are preferred.
Speed is not critical.
I have tried custom format but it is hard to extend it.
SAX-like parsers are too low level for such task.

The most important factor here is the file format to be passed on - whether you need to create a proxy class on the other side or if you simply need to read data on the other side, once the data format is known on the received side, the receiver side should know how to handle it.
Thus, it is best to use the data format which are well-known and widely used. Mostly for the reason of their widely-used virtue, such data formats would also normally have some 3rd party or build in library to help you creating your data structure files.
For this purpose, I will recommend you to use either JSON or XML data format. Python already have serializers for both:
XML: http://code.activestate.com/recipes/577268-python-data-structure-to-xml-serialization/
JSON : https://docs.python.org/3/library/json.html, http://www.diveintopython3.net/serializing.html, https://docs.python.org/2/library/json.html
You can also search some of other alternatives which I believe are also available apart from them.

Related

What are in-memory data structures in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I was given a coding challenge in which I have to parse a text file and build "A data structure in memory to work with." . I then have to perform descriptive statistics on it. So far I've parsed the text and build a dictionary containing all the needed data.
I haven't used SQlite or something similar because they specifically asked for data structures and not databases.
I am not sure if dictionary is correct here. So my question is: What are in-memory data sructures in python? I've tried the web but couldn't get an definitive answer.
An in memory data structure is one that is stored in RAM (as opposed to saved to disk or “pickled”). If you’re not using external programs that store to disk for you (like databases) and not explicitly storing to disk, you’ve created an in-memory data structure. Dicts, lists, sets, etc. are all data structures, and if you don’t save it to disk they’re in-memory data structures.

Advantages and disadvantages of the pickle, JSON and CSV methods for saving a dictionary [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've researched multiple methods for saving a dictionary, such as pickle, JSON and CSV, but I don't see anywhere with the benefits or restrictions of each of these methods.
pickle:
On the plus side, it can handle arbitrary objects (with varying levels of work). On the minus side the flat format is not human-readable, and it shouldn't be used with untrusted input. There are versioning issues, too; there are various different protocols defined.
json:
It's easy to move back and forth between some container (dict, list) and value (string and number) objects and JSON. It's also generally human-readable (subject to "pretty" formatting), widely used and well-supported by most (all?) languages. It can't handle arbitrary objects like pickling can, though.
csv:
Arguably the simplest format, but won't handle nesting well while remaining readable and easy to parse (it's probably best suited to persisting a simple table). There's generally more work to convert back and forth than JSON or pickle, too.

How to access all of a file's attributes through Python in Windows 7? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In a Windows 7 system you can right-click the sort columns to look at the details you want to view for a file and you get the following:
Question: Is there a way to access all of the attributes on that list for a given file using Python?
This is a bit long for a comment.
You are not likely to get a good answer because Microsoft makes this way too complicated, and their documentation on this topic is some of the worst that they have.
Everything is wrapped up in COM interfaces, and you really need the SDK installed to get all of the headers file needed to access these interfaces from a C style API.
To understand how it really works, you really need to start the Property System Overview
You will also want to read Property System Developers Guid
There is one C language answer that I know of for this topic on S/O, though clearly there could be others.
I know it is not a real answer, and it is certainly not Python -- but if you have the real motivation to dig into this, hopefully this is at least a little helpful.
Also not that these extended properties are poorly supported, and tend to disappear under many common usage patterns since they are not really part of the file -- e.g., copy the file using ftp -- lose the extended file attributes.

What is the best way to store data in Python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I'm creating a Skype bot for a group of friends, and I want to be able to have a somewhat login system for text-based games and storing information such as usernames, high scores, notes, friends list, messages, etc.
I was thinking of storing it in a text file named after the person's handle on Skype, however, I was wondering if there was a better way of doing it. Such as XML files.
I'd like to avoid SQL servers, and it's not like they're storing passwords so encryption won't be that much of a big deal. (I'd prefer local file storage. Something easily editable and delete-able)
I want to enable commands such as !note and !friends and !addfriend and !highscore and so on, but I need a method to save that information.
Have you considered pickle? It can store python objects (any object) to files so you can just load and use them.
import pickle
# Saving
data = [1,2,3,4]
pickle.dump(data, open("d:/temp/test.pkl","wb"))
# Loading
load = pickle.load(open("d:/temp/test.pkl","rb"))
For more info, read the docs
(Another option is the json module which serializes to json. It is used in a similar way, but can only save dictionaries, lists, strings and integers)

parsing C code using python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a huge C file (~100k lines) which I need to be able to parse. Mainly I need to be able to get details about individual fields of every structure (like field name and type for every field in the structure) from its definition. Is there a good(open source, which i can use in my code) way to do this already? Or should I write my own parser for this. If I have to write my own, can anyone suggest a good place to start? I have never worked with python before.
Thanks
Take a look at this link for an extensive list of parsing tools available for Python. Specifically, for parsing c code, try the pycparser
The right way to do this is almost certainly to interface with the front-end of an existing compiler, such as gcc, then work with the intermediate representation, rather than attempting to create your own parser, in any language.
However, pycparser, as suggested by Dhara might well be a good substitute, and definitely better than any attempt to roll your own.

Categories

Resources