daviddoria 334 Posting Virtuoso Featured Poster

Cool - is it available for download?

daviddoria 334 Posting Virtuoso Featured Poster

Thanks, I haven't used classes in python yet so this is a good example for me.. For future readers, there is a small typo. In the Pt class, the return statements say Point(...) when they should say Pt(...).

It's interesting this isn't in a library anywhere though, it seems like deg2rad, cart2sphere, etc type functions would be pretty handy.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

wow... hahaha, well at least my class was ok! Does

Log(filename)

have any use? Why is it not a compiler error?

Thanks for finding that ridiculous bug :)

daviddoria 334 Posting Virtuoso Featured Poster

Is there a library that has a function that will take (theta,phi) and return (x,y,z)?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

That is a linker error. You will not be able to find linker error's in the code. They have to do which which libraries you have your visual studio project linking to. I don't use visual studio so I can't tell you exactly, but you need to look in "project properties -> linking" or "libraries" or something like that and set which ever directX libraries you need to link to (I also don't use directX so I don't know which ones they are).

Can anyone give more specifics?

Dave

daviddoria 334 Posting Virtuoso Featured Poster

To redirect clog, I have been using this:

std::streambuf* clog_save;
	std::ofstream ofs;
	
	clog_save = std::clog.rdbuf();
	ofs.open(LogFilename.c_str());
	std::clog.rdbuf(ofs.rdbuf());
	
	cout << "Test cout." << endl;
	std::clog << "Test log." << endl;
	
	std::clog.rdbuf(clog_save);
	ofs.close();

However, it seems like bad "code reuse" practice to have to put this in every program I want to log. I tried to make this:

class Log
	{
		private:
			std::streambuf* clog_save;
			std::ofstream ofs;
		public:
			Log(const std::string &LogFilename)
			{
				clog_save = std::clog.rdbuf();
				ofs.open(LogFilename.c_str());
				std::clog.rdbuf(ofs.rdbuf());
			}
		
			~Log()
			{
				//reset the buffer
				std::clog.rdbuf(clog_save);
				ofs.close();
			}
	
	};

and do this to accomplish the same thing:

Log(LogFilename);
	cout << "Test cout." << endl;
	std::clog << "Test log." << endl;

but in this case "Test log." gets written to the screen instead of the file. This might have something to do with the scope of std::clog? I guess don't understand how these automatically instantiated classes (ie. cout, clog) work? Are they static or something?

Is there anyway to do this?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I have to call this many times:

MyScript.py --top='?_0.png'

where the 0 should be replaced with 0, 1, 2, 3, etc

I tried this:

for i in {0..3}; do 
echo MyScript.py --top="'?_${i}.png'";
MyScript.py --top="'?_${i}.png'";
done;

It seems to make the command look correct (ie the single quotes are around the filename and the i is expanded properly), but python does not interpret it the same way as if I run the first command on the command line directly.

Any idea why this would be?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

Thanks for the quick response! That'll work for now. However it doesn't scale particularly well (if I wanted a 1000x1000 matrix, I couldn't fill all those entries manually the first time!). Is there a way to initialize the matrix to a (0,0) pair for an arbitrary size?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I usually make a matrix like this

from Numeric import *
A=zeros([3,3])
print str(A[0,1]) #access an element

I would like to store a pair of values in each element, that is have a matrix where the (0,0) element is (2.3, 2.4), the (0,1) element is (5.6,6.7), etc.

Is this possible? I would like to access it using something like

A[0,1][0]
and
A[0,1][1]

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Shouldn't be too bad - split the string by '.', '?', and '!'. Then split by ' '. Keep any groups in which 'you' occurs.

daviddoria 334 Posting Virtuoso Featured Poster

What is your input and expected output?

daviddoria 334 Posting Virtuoso Featured Poster

cool, thanks

daviddoria 334 Posting Virtuoso Featured Poster

I found some code to parse command line arguments (is there an easier way? sed always seems so complicated...)

#!/bin/bash

for i in $*
do
	case $i in
    	--files=*)
		FILES=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
		;;
    	--default)
		DEFAULT=YES
		;;
    	*)
                # unknown option
		;;
  	esac
done

echo $FILES
echo "File 0:"
echo ${FILES[0]}

My question is, how do I loop through the filenames in FILES if I pass multiple files, ie

./Example.sh --files=*.txt

My example above (trying to access it like an array does not split the string at " ", so FILES[0] is the entire FILES.

Any ideas?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

thanks, that's a good start for now, but eventually it would be nice to do

--files *.txt --Size 4.2 --Duration 5

Dave

daviddoria 334 Posting Virtuoso Featured Poster

What I'm really trying to do is loop though a list of files

python ./MyScript.py *.jpg

should be able to do something like this

for MyFile in *.jpg
   print i

so I can operate on each file. How would I do that?

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I need to get an unknown number of file names from the command line, ie
./python MyScript.py --Files 1.txt 2.txt 3.txt

parser.add_option("-n", "--NumFiles", type="int",
                  help="Number of files")
parser.add_option("--Files", nargs=options.NumFiles,
                  help="a triple")
(options, args) = parser.parse_args()

Clearly this will not work because options.NumFiles is not available until after parse_args() is called. Is there a way to do this?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

haha yea, that is quite a challenging problem. You'd need at least an advanced degree in CS/EE or many many years of industry experience to even begin this problem.

daviddoria 334 Posting Virtuoso Featured Poster

Also, how do you handle required options? I saw a lot of threads about a philosophical debate of if options can be required, but no information on how to actually use them.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I see, I didn't realize that optparse was built in. I'm coming from c++, so almost nothing is built in, and adding libraries is usually pretty annoying.

daviddoria 334 Posting Virtuoso Featured Poster

I want to parse some simple arguments from the command line. This shows all of the arguments:

#!/usr/bin/python
import sys

#print all of the arguments
for arg in sys.argv:
	print arg

but I want to handle something like

--file a.txt

or

--x 12 --y 13 --z 14

I saw some libraries like optparse, but is there not something built in to do this simple type of parsing?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I tried to do this to redirect the clog output to a file:

ofstream ofs("file.log");
	clog.rdbuf(ofs.rdbuf());
	clog << "Logged." << endl;
	ofs.close();

It worked (the file contains the text), but the program segfaults. I read that you have to "restore" the original buffer, so I tried this

ofstream ofs("file.log");
	clog.rdbuf(ofs.rdbuf());
	clog << "Logged." << endl;
	ofs.close();
clog.rdbuf(clog.rdbuf());

But no change. Anyone know how to do this?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

You seem to have broken all of the rules at the same time....

daviddoria 334 Posting Virtuoso Featured Poster

A friend of mine said that python could be used to make simple GUIs (ie. a couple of buttons and a text box). I googled "python gui" and it looks like there are 30934 libraries to make GUIs. Is there a "standard" or "built in" one?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Why? As long as you dont have any conflicting names, you get to omit std:: all over the place. Most people know which things are members of the STL, so it doesn't really introduce ambiguity. Am I missing something?

daviddoria 334 Posting Virtuoso Featured Poster

What platform are you on (linux or windows)?

daviddoria 334 Posting Virtuoso Featured Poster

Is this an open source thing that we can download and try to compile? If so, please post a link.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

or you can put

using namespace std;

up above all of the code.

daviddoria 334 Posting Virtuoso Featured Poster

Can you make smaller but still compilable example of what is going wrong. We likely don't need to look at 100 lines to see the problem. Also, please use code tags.

daviddoria 334 Posting Virtuoso Featured Poster

oops, oops, I told you wrong, its pow(2,day), not pow(day,2)

daviddoria 334 Posting Virtuoso Featured Poster

inside the loop, just output
cout << "Current day: " << pow(day, 2);

daviddoria 334 Posting Virtuoso Featured Poster

Does anyone have a good 2 line summary of WHEN to use perl/python/bash/etc? To do easy-ish things, they can clearly all be used, but there is likely an ideology behind each that indicates WHEN/WHY to use them.

For example, I use VB if I want easy GUI, c++ if I want it to run very fast, etc.

I didn't see a "general programming" forum, so since I was most interested in when to use python I put it here.

Any thoughts?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

Some comments:
There's probably no reason to use global variables. If the program expands, then they make things very confusing.

string getname(void)
{
   cout << "Enter a name: ";
string Name;
    getline(cin, Name);
return Name;
  }

This is called "parallel sorting". I posted how to do it here:
http://www.daniweb.com/forums/thread181356.html

Dave

daviddoria 334 Posting Virtuoso Featured Poster

you should use

for(int day = 0; day < days; day++)

then just add 2^day (which is pow(day,2) in c++) to a running total.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

You are missing a '{' between these lines.

double taxAmount(double taxRate, int perExempt, int numberofPeople, double taxRate)

	double taxRate;
daviddoria 334 Posting Virtuoso Featured Poster

Can you extract and post the shortest example possible that will generate this problem? (so that we can compile it to track down the problem, but not have to look at 400+ lines of code)

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I tried this:

#include <iostream>
#include <vector>
#include <cstdlib>

#include <cmath>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

using namespace std;

int main(int argc, char* argv[])
{
	vector<vector<string> > Lists(2);
			
	po::options_description desc("Allowed options");
	desc.add_options()
			("help", "produce help message")
			("List0", po::value<vector<string> >(&Lists[0])->multitoken(), "List0.")
			("List1", po::value<vector<string> >(&Lists[1])->multitoken(), "List1.")
			;

	po::variables_map vm;
	po::store(po::parse_command_line(argc, argv, desc), vm);
	po::notify(vm); //assign the variables (if they were specified)

	if (vm.count("help"))
	{
		cout << desc << "\n";
		return 1;
	}

	for(unsigned int list = 0; list < Lists.size(); list++)
	{
		cout << "List: " << list << endl << "-----------" << endl;
		for(unsigned int item = 0; item < Lists[list].size(); item++)
		{
			cout << "List: " << list << " Item: " << item << " " << Lists[list][item] << endl;
		}
	}
	
	return 0;
}

but the output is:

List: 0
-----------
List: 0 Item: 0 1scan0.txt
List: 0 Item: 1 2scan0.txt
List: 0 Item: 2 --List1
List: 0 Item: 3 1scan1.txt
List: 0 Item: 4 2scan1.txt
List: 1
-----------

ie. it thinks --List1 is another option of --List0 !!

daviddoria 334 Posting Virtuoso Featured Poster

Sure, but how do you input them? Ie- how do you tell boost to start filling lists[1] instead of lists[0]?

daviddoria 334 Posting Virtuoso Featured Poster

Ok I have a better one for you:

I want to have multiple lists of files, like this:

--NumLists 3 --List1 a.txt b.txt c.txt --List2 d.txt e.txt --List3 f.txt g.txt

How would you handle that? (The number of lists is not known until the NumLists param is given)

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I have a lot of other arguments too, I just took out this one part.

In any case, the solution is:

("FileList", po::value<vector<string> >()->multitoken(), "List of files.")
daviddoria 334 Posting Virtuoso Featured Poster

Does anyone use boost program_options? What I'm trying to do is this:

--Files 1.jpg 2.jpg 3.jpg

The only thing I know how to do is

--File1 1.jpg --File2 2.jpg --File3 3.jpg

Does anyone know how to do this, and maybe store them in a vector<string>?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

It seems like you have just posted your homework assignment. Please give a try and check back here if you have any problems.

daviddoria 334 Posting Virtuoso Featured Poster

I am trying to do this

#!/bin/bash

for i in 1 2 3 4 5; do 
	File1="$i.txt"
	File2="$i.obj"
	./Test $File1 $File2
 #save i and the result of the program somehow
done;

For example, at the end I would like to have a file like this

1 3.4
2 4.55
3 56.0
4 22.3
5 8.9

Where the second column is the "output" of the c++ program. The problem is the program has other outputs, ie

Starting program....
Doing stuff ....
3.4

Anyone ever wrap a program in a loop like this before and care about saving the output?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

ScienceNerd, please use code tags, it helps make the cod readable.

Dave

daviddoria 334 Posting Virtuoso Featured Poster
daviddoria 334 Posting Virtuoso Featured Poster

bah cin.flush().... I always forget that! I guess I don't understand why it isn't done automatically.

That \r is exactly what I was looking for, but now that I think about it I probably can't do exactly what I wanted. As I mentioned the boost progress bar writes
*
then replaces it with
**
then
***
etc. , so I would have to allow it to keep writing *'s in a line at the same time as outputting my counter with \r, and that doesn't seem do-able.

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

hmm, in trying that, I discovered this:

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
	for(int counter = 1000; counter < 10000; counter++)
	{
		//cout << "hi" << endl; // this works
		cout << "hi"; //this doesn't work
		sleep(1);
	}
return 0;
}

If I put an endl at the end of the output, every 1 second I see a new line that says "hi". If I don't put the endl, I would expect to see

hihihihihihihi

but instead I see nothing! Why would that be? I was also seeing nothing with the \r, but I bet it's the same reason.

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I have used boost's progress bar (it outputs ***** from 0 to 100% complete) to track the progress of long loops. It would also be nice to be able to see a counter along with that, like if I run 1000 tests, the *'s will go from 0 to 100%, but I would also like to see
4 tests pass
5 tests pass

etc. without having a new line for each, ie the 5 would simply replace the 4, so the progress bar could remain visible while I am updated about the number of tests that have passed.

Has anyone seen anything that does something like this?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I thought threads were closed if they were not supposed to be posted in any more? I thought I was helping by posting a solution so that the thread could be closed.

daviddoria 334 Posting Virtuoso Featured Poster

I mean either the problem is impossible, in which case, well, it is impossible (ie the screen coords are not actually a function of the real coords), or, it is - there is really no middle ground!

To see how many pixels (in x) are needed to represent 1 unit on the real boards, just do this:
you know 10 maps to 100, and 175 maps to 200. That means for every 90 screen pixels, you have 25 units on the board. You can do the same thing for y. Then you just need to know where you are starting ( (0,0) perhaps?) and then you just multiply the board coords by those ratios and you should get the screen coords. Of course they will not be integers, so you will have to round.

Did I read the problem correctly?

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Here are some of the answers:

What is the output of printf("%d")

It will output some junk signed integer value because you never told it which signed integer to output.

Difference between "vector" and "array"?

A vector is a STL container that has the same functionality as an array, but you check the validity of an index using .at() and you can get the length with .size(). You can also use STL iterators to potentially convert a vector into a different STL container.

Can we generate a C++ source code from the binary file?

Sounds hard, if not impossible.

What are inline functions?

Functions that the compiler knows to optimize. I think most compiler usually figure out what to inline by themselves these days.

Details about profiling?

I use valgrind's cachegrind. Works really well/easily.

tux4life commented: Nice explanation ! +1