473,396 Members | 1,963 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,396 software developers and data experts.

Create new instance of Python class in C

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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Sep 9 '05 #1
5 1909
djw
Sybren Stuvel wrote:
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
Sep 9 '05 #2
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Sep 9 '05 #3
On Fri, 9 Sep 2005 17:19:21 +0200, Sybren Stuvel <sy*******@YOURthirdtower.com.imagination> wrote:
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.

--
Email: zen19725 at zen dot co dot uk
Sep 10 '05 #4
On Fri, 9 Sep 2005 18:50:26 +0200, Sybren Stuvel <sy*******@YOURthirdtower.com.imagination> wrote:
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.


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

--
Email: zen19725 at zen dot co dot uk
Sep 10 '05 #5
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Sep 10 '05 #6

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

Similar topics

5
by: python newbie | last post by:
hey, okay, I'm trying to figure out why my books: Quick Python, Python in a Nutshell, Python Cookbook and Learning Python don't say anything about the weird behavior of a list when you have one as...
3
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
2
by: Helge | last post by:
I wonder how this can be accomplished: I've got a list, containing several strings. The strings look like this: I would like to create one object for each item in the list, and the name...
3
by: Robert Oschler | last post by:
Hello, I am a Python newbie (by experience, not chronologically :) ), so if any of this doesn't make sense my apologies in advance. I am reading the chapter in The Python Cookbook on databases...
4
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Sandra-24 | last post by:
Can you create an instance of a subclass using an existing instance of the base class? Such things would be impossible in some languages or very difficult in others. I wonder if this can be done...
6
by: Pierre Rouleau | last post by:
Hi all, Is there any reason that under Python you cannot instantiate the object class and create any attributes like you would be able for a normal class? Python 2.4.2 (#67, Sep 28 2005,...
4
by: Gre7g Luterman | last post by:
I suppose I was lulled into complacency by how Python makes so many things look like classes, but I'm starting to realize that they're not, are they? I'm writing a C program which handles Python...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.