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

Manipulating sets from the 2.4 C API?

I just looked through the Python/C API reference for 2.4.3 and didn't
see anything about using sets. I'd been expecting things like PySet_New,
PySet_Check etc.

If I want to handle sets should I just use a dictionary's keys and
ignore the values, or is there some more explicit set support somewhere
I'm not seeing?

Thanks,
Dave Opstad
Apr 11 '06 #1
4 1197
On Tue, Apr 11, 2006 at 09:29:10AM -0700, Dave Opstad wrote:
I just looked through the Python/C API reference for 2.4.3 and didn't
see anything about using sets. I'd been expecting things like PySet_New,
PySet_Check etc.
There is a public C API starting in 2.5, the progression for sets was
pure-python in 2.3, C in 2.4, and polished in 2.5 after it was better
understood how people use them. If Hettinger is around he'll correct me
if that explanation is off.
If I want to handle sets should I just use a dictionary's keys and
ignore the values, or is there some more explicit set support somewhere
I'm not seeing?


There are people that use sets from C-code but I don't know if they use
the slotted methods, the 2.5 version, or a special build.

-Jack
Apr 11 '06 #2
Dave Opstad wrote:
If I want to handle sets should I just use a dictionary's keys and
ignore the values, or is there some more explicit set support somewhere
I'm not seeing?


Indeed, there is. To create a new set, do

PyObject_Call(PySet_Type, "");

To, say, invoke the add method, do

PyObject_CallMethod(s, "add", "O", o);

HTH,
Martin
Apr 11 '06 #3
Em Ter, 2006-04-11 Ã*s 18:55 +0200, "Martin v. Löwis" escreveu:
Dave Opstad wrote:
If I want to handle sets should I just use a dictionary's keys and
ignore the values, or is there some more explicit set support somewhere
I'm not seeing?


Indeed, there is. To create a new set, do

PyObject_Call(PySet_Type, "");

To, say, invoke the add method, do

PyObject_CallMethod(s, "add", "O", o);


I don't know much about the C API, but I'll ask anyway: the methods,
say, PySet, would be included for clarity/brevity or for performance
reasons?

--
Felipe.

Apr 11 '06 #4
Dave Opstad wrote:
I just looked through the Python/C API reference for 2.4.3 and didn't
see anything about using sets. I'd been expecting things like PySet_New,
PySet_Check etc.
In Py2.4, there was not a custom set C API because the module was still
ungoing significant development. For 2.4, the best way to access sets
from C is to use the abstract API:

s=PyObject_CallObject(PySet_Type, NULL) // s=set()
PyObject_TypeCheck(o, &PySet_Type) // isinstance(o, set)
PyObject_GetIter(s) // iter(s)
PyObject_Hash(f) // hash of frozenset
PyObject_Length(s) // len(s)
PyNumber_Subtract(s,t) // s - t
PyObject_CallMethod(s, "pop", NULL) // s.pop()

If I want to handle sets should I just use a dictionary's keys and
ignore the values, or is there some more explicit set support somewhere
I'm not seeing?


In Py2.4, set objects are based on dictionaries so the performance is
about the same, so the only advantage of using set objects instead of
dictionaries is that they provide methods like union, intersection, etc
which are not defined for dictionaries.

In Py2.5, set objects are no longer based on dictionaries and tend to
have better speed/space performance than equivalent dictionary code.
There is also a more full-blown C API including PySet_New(),
PySet_Size(), PySet_Add(), PySet_Contains(), ...

Apr 11 '06 #5

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

Similar topics

21
by: Raymond Hettinger | last post by:
I've gotten lots of feedback on the itertools module but have not heard a peep about the new sets module. * Are you overjoyed/outraged by the choice of | and & as set operators (instead of + and...
5
by: Raymond Hettinger | last post by:
For Py2.4, I'm working on a C implementation of Sets.py with the possibility of having a set() type as a builtin. There is a question as whether the current design for set of sets has been useful....
1
by: Gerrit Holl | last post by:
Brett C. wrote: > Sets now at blazing C speeds! > ----------------------------- > Raymond Hettinger implemented the sets API in C! The new built-ins are > set (which replaces sets.Set) and...
7
by: Steve | last post by:
This post has two parts. First is my feedback on sets. (Hello? Last summer called, they want their discussion thread back...) Second is some questions about my implementation of a partition...
2
by: James Dean | last post by:
Could anybody tell me the most efficient way to set the bits in a 1Bpp class. I am reading in byte lines of an image. The byte array tells me what what bits are turned on or off.....i do bit...
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
11
by: Prateek | last post by:
I have 3 variable length lists of sets. I need to find the common elements in each list (across sets) really really quickly. Here is some sample code: # Doesn't make sense to union the sets -...
1
by: JosAH | last post by:
Greetings, Introduction This week I'll write a bit about generics (those funny angular brackets). I need an example and decided to use sets and some of their operations. This weeks' article...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.