473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

From Python to c++

This is actually a c++ problem. Got no satisfying answer on comp.lang.c++.
Maybe you can help better because I know, there are many c++-converts ;).

Suppose you've got the following list (first line has field names, second
line has types and any row after is data):

csv = "name,age,place\nstring,int,string\nMac,25,Zurich\ nMike,55,Oslo"

and you would like to turn it into a dictionary:

parsed = {
"name":["Mac", "Mike"],
"age":[25, 55],
"place":["Zurich", "Oslo"]
}

A trivial task in Python. In C++ it is cumbersome. I was thinking to put
the parsed data into a map:

map<string, vector<???> >

I have no problem with the key (string) but the value (vector) needs to be
of varying type. I think C++-STL does not allow what I want.

The following proposal is useless:

[...]
A simple solution, make two maps:

std::map<std::string, std::vector<std::string> > result_name;
std::map<std::string, std::vector<int> > result_age;
[...]

I want to build the map of lists dynamically - it can have many fields or
just one... and I would like to have simple interface (as Python offers).

- I cannot import python into the c++ environment (Borland Developer 2006)
and I know no one who was able to.

- I cannot write (*sigh*) the app in Python.

Any suggestions are very welcome!
Regards,
Marco (Forced to code in c++ again let me estimate the simplicity of
python)
Mar 21 '06 #1
3 1444
> Any suggestions are very welcome!

Are you allowed to use any boost libraries? If so then boost::any would
probably help. The boost::any object can contain any object type. So you
could have a single container that looked like the following:

std::map< std::string, std::vector<boost::any> > result;

Since you know the type, you would use boost::any_cast to convert it to
that type. You can find information about it here:

http://www.boost.org/doc/html/any.html

-Farshid
Mar 21 '06 #2
Marco Aschwanden wrote:
This is actually a c++ problem. Got no satisfying answer on comp.lang.c++.
Maybe you can help better because I know, there are many c++-converts ;).

Suppose you've got the following list (first line has field names, second
line has types and any row after is data):

csv = "name,age,place\nstring,int,string\nMac,25,Zurich\ nMike,55,Oslo"

and you would like to turn it into a dictionary:

parsed = {
"name":["Mac", "Mike"],
"age":[25, 55],
"place":["Zurich", "Oslo"]
}

A trivial task in Python. In C++ it is cumbersome. I was thinking to put
the parsed data into a map:

map<string, vector<???> >

I have no problem with the key (string) but the value (vector) needs to be
of varying type. I think C++-STL does not allow what I want.


This is so scary, I probably shouldn't post this. It sounds from your
description, you really want RTTI. I haven't done this in a long
while, so I'm not sure there is an easier way. But the program below
works and seems like it may be what you are asking for.

n
--
#include <stdio.h>
#include <vector>

struct Any {
virtual ~Any() { };
};

typedef std::vector<Any*> List;

class Str : public Any {
public:
Str(const char *s) : value_(s) {};
operator const char*() const {
return value_;
}
private:
const char *value_;
};

class Int : public Any {
public:
Int(int s) : value_(s) {};
operator int() const {
return value_;
}
private:
int value_;
};

int main(int argc, char **argv) {
Str anyone = "someone";
Int my_age = 5;
List list;
list.push_back(&anyone);
list.push_back(&my_age);

for (List::const_iterator i = list.begin(); i != list.end();
i++) {
const Int *iptr;
const Str *str;
if ((str = dynamic_cast<const Str*>(*i)))
printf("string: %s\n", (const char*) *str);
else if ((iptr = dynamic_cast<const Int*>(*i)))
printf("int: %d\n", (int) *iptr);
else
printf("clueless: %p\n", *i);
}

return 0;
}

Mar 22 '06 #3
On Wed, 22 Mar 2006 08:11:24 +0100, nn******@gmail.com
<nn******@gmail.com> wrote:
This is so scary, I probably shouldn't post this. It sounds from your
description, you really want RTTI. I haven't done this in a long
while, so I'm not sure there is an easier way. But the program below
works and seems like it may be what you are asking for.


Perfect! That is precisely what I was looking for. I can easily adapt it
for my needs! Thanks a lot.

Regards,
Marco
Mar 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test ...
2
by: satish | last post by:
Hello all, I have a shared object executable viz. *cable* which I execute as follows : $ ansyscust71 -custom cable -p ANSYSRF **ansyscust71 is a shell script and is a part of a software...
2
by: Jorgen Grahn | last post by:
I couldn't think of a good solution, and it's hard to Google for... I write python command-line programs under Win2k, and I use the bash shell from Cygwin. I cannot use Cygwin's python package...
1
by: John fabiani | last post by:
Hi, I have a script that is not running on a freeBSD 4.4 with Python 2.1 that uses ftplib. I'm wondering if it's possible to update only the ftplib module from 2.1 to 2.3. I only ask because I...
13
by: Ajay | last post by:
hi! can you call a Python application from a Java program? does this require any additional package to be installed? thanks cheers
9
by: Nico Grubert | last post by:
Hi there, Background of this question is: I want to convert all words <word> except 'and' / 'or' / 'and not' from a string into '*<word>*'. Example: I have the following string: "test and...
7
by: alexandre_irrthum | last post by:
Hi there, I am trying to use pyserial to read data from a temperature logger device (T-logger). T-logger is based on the DS1615 temperature recorder chip (Dallas Semiconductor). According to the...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
1
by: Martin Rubey | last post by:
Dear all, I'm trying to call from common lisp functions written for Sage (www.sagemath.org), which in turn is written in python. To do so, I tried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.