Create new instance of Python class in C

S

Sybren Stuvel

Hi people,

I'm creating a program that can solve and create Sudoku puzzles. My
creation function needs to make a lot of copies of a puzzle. Until
now, I used copy.deepcopy(), but that's too slow. I want to implement
such a copying function in C and use that instead. My idea about this
is:

- Get the data from a puzzle (a list containing lists containing
strings) and make a copy of it. That's coded already.

- Create a new SodokuPuzzle instance and assign the data to it.

That last step can be done by passing the data to the constructor, so
that's easy too once I know how to do that in C. My question is: how
do I create a new instance in C of a class written in Python? I've
searched Google, but found nothing of help.

Sybren
 
D

djw

Sybren said:
Hi people,

I'm creating a program that can solve and create Sudoku puzzles. My
creation function needs to make a lot of copies of a puzzle. Until
now, I used copy.deepcopy(), but that's too slow. I want to implement
such a copying function in C and use that instead. My idea about this
is:

- Get the data from a puzzle (a list containing lists containing
strings) and make a copy of it. That's coded already.

- Create a new SodokuPuzzle instance and assign the data to it.

That last step can be done by passing the data to the constructor, so
that's easy too once I know how to do that in C. My question is: how
do I create a new instance in C of a class written in Python? I've
searched Google, but found nothing of help.

Sybren
Personally, I would try Psyco first, and consider Pyrex next. Are you
sure your algorithm can't be optimized first, before you start trying to
write this in C?

-Don
 
S

Sybren Stuvel

djw enlightened us with:
Personally, I would try Psyco first, and consider Pyrex next.

Ok, I'll take a look at those.
Are you sure your algorithm can't be optimized first, before you
start trying to write this in C?

I'm sure there will be optimizations, but profiling showed that the
copying of the puzzles took the most time. Since the copy.deepcopy()
function is implemented it Python, I'd thought it would get quite a
speed boost when done in C instead.

As a side-question: how would I go about compiling my C module in
Windows, if I want to ship a Windows version?

Sybren
 
P

phil hunt

Hi people,

I'm creating a program that can solve and create Sudoku puzzles. My
creation function needs to make a lot of copies of a puzzle.

Why do you need to maske lots of copies? And when you say "lots of"
what numbers do you mean? 100? 1000000?

The reason I ask is I recently wrote a program to solve Sudoku
puzzles, and IIRC it didn't make copies at all.
 
P

phil hunt

djw enlightened us with:

Ok, I'll take a look at those.


I'm sure there will be optimizations, but profiling showed that the
copying of the puzzles took the most time. Since the copy.deepcopy()
function is implemented it Python, I'd thought it would get quite a
speed boost when done in C instead.

Can you use a different algorithm that doesn't make so many copies?
 
S

Sybren Stuvel

phil hunt enlightened us with:
Why do you need to maske lots of copies?

The puzzles are stored in a NxN list of strings. The strings contain
all the numerals that that block can contain. So a 9x9 puzzle contains
81 strings "123456789" when it's "empty".

My creation function picks a block that isn't a given, and fixes it to
one of it's possible values. It then tries to solve the puzzle. If it
works, it's done creating the puzzle. If it doesn't work, it starts
over again in a recursive manner.

The solver solves the puzzle in-place. That means that if I want to
keep the original puzzle (the one that could be solved), I have to
make a copy.
And when you say "lots of" what numbers do you mean? 100? 1000000?

That depends a lot. The parts "picks a block that isn't a given" and
"fixes it to one if it's possible values" are both randomized.
Sometimes it's 100, sometimes it's 50000.
The reason I ask is I recently wrote a program to solve Sudoku
puzzles, and IIRC it didn't make copies at all.

My solver doesn't create copies either. The creator does.

I think I'll change my algorithm to just solve the puzzle and don't
care about the original minimal puzzle that could be solved. I'll
create a fully solved puzzle, and then use another routine to remove
givens at random until the required number of givens is left. Of
course, still maintaining solvability.

That last part would require copying, but not as much as in the
current situation.

Sybren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top