My previous message just went up -- sorry for the mangled formatting. Here it
is properly formatted:
I want to write a list of 64-bit integers to a binary file. Every example I
have seen in my research converts it to .txt, but I want it in binary. I wrote
this code, based on some earlier wor
Dieter, thanks for your comment that:
* In your code, `offset` is `0`, `1`, `2`, ...
but it should be `0 *8`, `1 * 8`, `2 * 8`, ...
But you concluded with essentially the same solution proposed by MRAB, so that
would obviate the need to write item by item because it writes the whole buffer
at o
the extra overhead, and it's more
difficult yet if I'm using the Python integer output in a C program. Your
solution solves those problems.
Oct 2, 2023, 17:11 by python-list@python.org:
> On 2023-10-01 23:04, Jen Kris via Python-list wrote:
>
>>
>> Iwant to write
Iwant to write a list of 64-bit integers to a binary file. Everyexample I have
seen in my research convertsit to .txt, but I want it in binary. I wrote this
code,based on some earlier work I have done:
buf= bytes((len(qs_array)) * 8)
foroffset in range(len(qs_array)):
item_to_write= byt
Thanks to everyone who answered this question. Your answers have helped a lot.
Jen
Mar 27, 2023, 14:12 by m...@wichmann.us:
> On 3/26/23 17:53, Jen Kris via Python-list wrote:
>
>> I’m asking all these question because I have worked in a procedural style
>> for many
Cameron,
Thanks for your reply. You are correct about the class definition lines – e.g.
class EqualityConstraint(BinaryConstraint). I didn’t post all of the code
because this program is over 600 lines long. It's DeltaBlue in the Python
benchmark suite.
I’ve done some more work since thi
the choose_method in the UrnaryConstraint
class because of "super(BinaryConstraint, self).__init__(strength)" in step 2
above?
Thanks for helping me clarify that.
Jen
Mar 26, 2023, 18:55 by hjp-pyt...@hjp.at:
> On 2023-03-26 19:43:44 +0200, Jen Kris via Python-list wrote:
&g
Thanks to Richard Damon and Peter Holzer for your replies. I'm working through
the call chain to understand better so I can post a followup question if
needed.
Thanks again.
Jen
Mar 26, 2023, 19:21 by rich...@damon-family.org:
> On 3/26/23 1:43 PM, Jen Kris via Python-li
The base class:
class Constraint(object):
def __init__(self, strength):
super(Constraint, self).__init__()
self.strength = strength
def satisfy(self, mark):
global planner
self.choose_method(mark)
The subclass:
class UrnaryConstraint(Constraint):
def __init__
I wrote my previous message before reading this. Thank you for the test you
ran -- it answers the question of performance. You show that re.finditer is
30x faster, so that certainly recommends that over a simple loop, which
introduces looping overhead.
Feb 28, 2023, 05:44 by li...@tompass
Using str.startswith is a cool idea in this case. But is it better than regex
for performance or reliability? Regex syntax is not a model of simplicity, but
in my simple case it's not too difficult.
Feb 27, 2023, 18:52 by li...@tompassin.net:
> On 2/27/2023 9:16 PM, avi.e.gr...@gmail.com
hings that are far from the same such as matching two
> repeated words of any kind in any case including "and and" and "so so" or
> finding words that have multiple doubled letter as in the stereotypical
> bookkeeper. In those cases, you may want even more than offset
I haven't tested it either but it looks like it would work. But for this case
I prefer the relative simplicity of:
example = 'X - abc_degree + 1 + qq + abc_degree + 1'
find_string = re.escape('abc_degree + 1')
for match in re.finditer(find_string, example):
print(match.start(), match.end())
string.count() only tells me there are N instances of the string; it does not
say where they begin and end, as does re.finditer.
Feb 27, 2023, 16:20 by bobmellow...@gmail.com:
> Would string.count() work for you then?
>
> On Mon, Feb 27, 2023 at 5:16 PM Jen Kris via Python-list <
I went to the re module because the specified string may appear more than once
in the string (in the code I'm writing). For example:
a = "X - abc_degree + 1 + qq + abc_degree + 1"
b = "abc_degree + 1"
q = a.find(b)
print(q)
4
So it correctly finds the start of the first instance, but not
Yes, that's it. I don't know how long it would have taken to find that detail
with research through the voluminous re documentation. Thanks very much.
Feb 27, 2023, 15:47 by pyt...@mrabarnett.plus.com:
> On 2023-02-27 23:11, Jen Kris via Python-list wrote:
>
>>
When matching a string against a longer string, where both strings have spaces
in them, we need to escape the spaces.
This works (no spaces):
import re
example = 'abcdefabcdefabcdefg'
find_string = "abc"
for match in re.finditer(find_string, example):
print(match.start(), match.end())
Tha
Yes, in fact I asked my original question – "I discovered something about
Python array handling that I would like to clarify" -- because I saw that
Python did it that way.
Jan 14, 2023, 15:51 by ros...@gmail.com:
> On Sun, 15 Jan 2023 at 10:32, Jen Kris via Python-list
>
a)
> 3
> sys.getrefcount(b)
> 3
> c = b
> d = a
> sys.getrefcount(a)
> 5
> sys.getrefcount(d)
> 5
> del(a)
> sys.getrefcount(d)
> 4
> b = "something else"
> sys.getrefcount(d)
> 3
>
> So, in theory, you could carefully write your code to C
34
> >>> b=1234
> >>> a is b
> False
>
> Not sure what happens if you manipulate the data referenced by 'b' in the
> first example thinking you are changing something referred to by 'a' ... but
> you might be smart to NOT th
other data
> structure. Of course, if anything else is accessing the result in the
> original in between, it won't work.
>
> Just FYI, a similar analysis applies to uses of the numpy and pandas and
> other modules if you get some kind of object holding indices to a series s
e precise, an operation on one name will be reflected in the other name.
The difference is in the names, not the pointers. Each name has the same
pointer in my example, but operations can be done in Python using either name.
Jan 11, 2023, 09:13 by r...@roelschroeven.net:
> Op 11/01/2
Yes, I did understand that. In your example, "a" and "b" are the same pointer,
so an operation on one is an operation on the other (because they’re the same
memory block). My issue in Python came up because Python can dynamically
change one or the other to a different object (memory block) so
matrix operations, you might use NumPy. Its
> arrays and matrices are heavily optimized for fast processing and provide
> many useful operations on them. No use calling out to C code yourself when
> NumPy has been refining that for many years.
>
> On 1/10/2023 4:10 PM
> On Wed, 11 Jan 2023 at 07:14, Jen Kris via Python-list
> wrote:
>
>>
>> I am writing a spot speedup in assembly language for a short but
>> computation-intensive Python loop, and I discovered something about Python
>> array handling that I would like to clarify.
I am writing a spot speedup in assembly language for a short but
computation-intensive Python loop, and I discovered something about Python
array handling that I would like to clarify.
For a simplified example, I created a matrix mx1 and assigned the array arr1 to
the third row of the matrix:
Thanks for your reply. Victor's article didn't mention ctypes extensions, so I
wanted to post a question before I build from source.
Nov 14, 2022, 14:32 by ba...@barrys-emacs.org:
>
>
>> On 14 Nov 2022, at 19:10, Jen Kris via Python-list
>> wrote:
>>
>
In September 2021, Victor Stinner wrote “Debugging Python C extensions with
GDB”
(https://developers.redhat.com/articles/2021/09/08/debugging-python-c-extensions-gdb#getting_started_with_python_3_9).
My question is: with Python 3.9+, can I debug into a C extension written in
pure C and call
That's great. It clarifies things a lot for me, particularly re ref count for
new references. I would have had trouble if I didn't decref it twice.
Thanks very much once again.
Sep 30, 2022, 12:18 by pyt...@mrabarnett.plus.com:
> On 2022-09-30 17:02, Jen Kris wrote:
>
>>
>> Thanks very
Thanks very much for your detailed reply. I have a few followup questions.
You said, “Some functions return an object that has already been incref'ed
("new reference"). This occurs when it has either created a new object (the
refcount will be 1) or has returned a pointer to an existing objec
;> value_ptr, NULL);
>>
>> if (p_seed_calc == 0x0){
>> PyErr_Print();
>> return 1;}
>>
>> //Prepare return values
>> long return_val = PyLong_AsLong(p_seed_calc);
>>
>> return return_val;
>> }
>>
>> So I in
>
> So I incremented the reference to all objects in Get_LibModules, but I still
> get the same segfault at PyObject_CallFunctionObjArgs. Unfortunately,
> reference counting is not well documented so I’m not clear what’s wrong.
>
>
>
>
> Sep 29, 2022, 10:06 by p
cremented the reference to all objects in Get_LibModules, but I still
get the same segfault at PyObject_CallFunctionObjArgs. Unfortunately,
reference counting is not well documented so I’m not clear what’s wrong.
Sep 29, 2022, 10:06 by pyt...@mrabarnett.plus.com:
> On 2022-09-29 16:5
Recently I completed a project where I used PyObject_CallFunctionObjArgs
extensively with the NLTK library from a program written in NASM, with no
problems. Now I am on a new project where I call the Python random library. I
use the same setup as before, but I am getting a segfault with random
Thanks for PySequence_InPlaceConcat, so when I need to extend I'll know what to
use. But my previous email was based on incorrect information from several SO
posts that claimed only the extend method will work to add tuples to a list. I
found that's wrong -- even my own Python code uses the a
Chris, you were right to focus on the list pDictData itself. As I said, that
is a list of 2-tuples, but I added each of the 2-tuples with PyList_Append, but
you can only append a tuple to a list with the extend method. However, there
is no append method in the C API as far as I can tell -- h
pDictData, despite the name, is a list of 2-tuples where each 2-tuple is a
dictionary object and a string.
Mar 12, 2022, 13:41 by ros...@gmail.com:
> On Sun, 13 Mar 2022 at 08:25, Jen Kris via Python-list
> wrote:
>
>> PyObject* slice = PySlice_New(PyLong_FromLong(0)
s.com:
> On 2022-03-12 21:24, Jen Kris via Python-list wrote:
>
>> I have a C API project where I have to slice a list into two parts.
>> Unfortunately the documentation on the slice objects is not clear enough for
>> me to understand how to do this, and I haven’t found e
I have a C API project where I have to slice a list into two parts.
Unfortunately the documentation on the slice objects is not clear enough for me
to understand how to do this, and I haven’t found enough useful info through
research. The list contains tuple records where each tuple consists
e way as ‘’.join, and if not then (2) how
>> can I strip characters from a string object in the C API?
>>
> Your Python code is joining the list with a space as the separator.
>
> The equivalent using the C API is:
>
> PyObject* separator;
> PyObject*
The PyObject str_sentence is a string representation of a list. I need to
convert the list to a string like "".join because that's what the library call
takes.
Mar 7, 2022, 09:09 by ros...@gmail.com:
> On Tue, 8 Mar 2022 at 04:06, Jen Kris via Python-list
> wrote:
&g
the same way as ‘’.join, and if not then (2) how can I
strip characters from a string object in the C API?
Thanks.
Mar 6, 2022, 17:42 by pyt...@mrabarnett.plus.com:
> On 2022-03-07 00:32, Jen Kris via Python-list wrote:
>
>> I am using the C API in Python 3.8 with the nltk
I am using the C API in Python 3.8 with the nltk library, and I have a problem
with the return from a library call implemented with
PyObject_CallFunctionObjArgs.
This is the relevant Python code:
import nltk
from nltk.corpus import gutenberg
fileids = gutenberg.fileids()
sentences = gutenberg
Yes, that works. This is my first day with C API dictionaries. Now that
you've explained it, it makes perfect sense. Thanks much.
Jen
Feb 14, 2022, 17:24 by ros...@gmail.com:
> On Tue, 15 Feb 2022 at 12:07, Jen Kris via Python-list
> wrote:
>
>>
>> I created a
I created a dictionary with the Python C API and assigned two keys and values:
PyObject* this_dict = PyDict_New();
const char *key = "key1";
char *val = "data_01";
PyObject* val_p = PyUnicode_FromString(val);
int r = PyDict_SetItemString(this_dict, key, val_p);
// Add another k-v pair
key = "
Thank you for that suggestion. It allowed me to replace six lines of code with
one. :)
Feb 10, 2022, 12:43 by pyt...@mrabarnett.plus.com:
> On 2022-02-10 20:00, Jen Kris via Python-list wrote:
>
>> With the help of PyErr_Print() I have it solved. Here is the final code
erday.
Thanks much for your help.
Jen
Feb 9, 2022, 18:43 by pyt...@mrabarnett.plus.com:
> On 2022-02-10 01:37, Jen Kris via Python-list wrote:
>
>> I'm using Python 3.8 so I tried your second choice:
>>
>> pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem);
>
With the help of PyErr_Print() I have it solved. Here is the final code (the
part relevant to sents):
Py_ssize_t listIndex = 0;
pListItem = PyList_GetItem(pFileIds, listIndex);
pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
pListStr = PyBytes_AS_STRING(pListStrE
I'll do that and post back tomorrow. The office is closing and I have to leave
now (I'm in Seattle). Thanks again for your help.
Feb 9, 2022, 17:40 by songofaca...@gmail.com:
> On Thu, Feb 10, 2022 at 10:37 AM Jen Kris wrote:
>
>>
>> I'm using Python 3.8 so I tried your second choice:
>>
>
ple/amliu/vrut/python/ext/buildValue.html,
>> PyBuildValue "builds a tuple only if its format string contains two or more
>> format units" and that doc contains examples.
>>
>>
>> Yes, and PyObject_Call accept tuple, not str.
>>
>>
>> ht
_Call accept tuple, not str.
>
>
> https://docs.python.org/3/c-api/call.html#c.PyObject_Call
>
>>
>> Feb 9, 2022, 16:52 by songofaca...@gmail.com:
>>
>> On Thu, Feb 10, 2022 at 9:42 AM Jen Kris via Python-list
>> wrote:
>>
>>
>> I have everythi
/vrut/python/ext/buildValue.html, PyBuildValue
"builds a tuple only if its format string contains two or more format units"
and that doc contains examples.
Feb 9, 2022, 16:52 by songofaca...@gmail.com:
> On Thu, Feb 10, 2022 at 9:42 AM Jen Kris via Python-list
> wrote:
>
This is a follow-on to a question I asked yesterday, which was answered by
MRAB. I'm using the Python C API to load the Gutenberg corpus from the nltk
library and iterate through the sentences. The Python code I am trying to
replicate is:
from nltk.corpus import gutenberg
for i, fileid in en
Thank you for clarifying that. Now on to getting the iterator from the method.
Jen
Feb 8, 2022, 18:10 by pyt...@mrabarnett.plus.com:
> On 2022-02-09 01:12, Jen Kris via Python-list wrote:
>
>> I am using the Python C API to load the Gutenberg corpus from the nltk
>> l
I am using the Python C API to load the Gutenberg corpus from the nltk library
and iterate through the sentences. The Python code I am trying to replicate is:
from nltk.corpus import gutenberg
for i, fileid in enumerate(gutenberg.fileids()):
sentences = gutenberg.sents(fileid)
et
An ASCII string will not work. If you convert 32894 to an ascii string you
will have five bytes, but you need four. In my original post I showed the C
program I used to convert any 32-bit number to 4 bytes.
Feb 2, 2022, 10:16 by python-list@python.org:
> I applaud trying to find the right
It's not clear to me from the struct module whether it can actually auto-detect
endianness. I think it must be specified, just as I had to do with
int.from_bytes(). In my case endianness was dictated by how the four bytes
were populated, starting with the zero bytes on the left.
Feb 1, 202
ig endian.
However, if anyone on this list knows how to pass data from a non-Python
language to Python in multiprocessing.shared_memory please let me (and the
list) know.
Thanks.
Feb 1, 2022, 14:20 by ba...@barrys-emacs.org:
>
>
>> On 1 Feb 2022, at 20:26, Jen Kris via Pyth
I am using multiprocesssing.shared_memory to pass data between NASM and Python.
The shared memory is created in NASM before Python is called. Python connects
to the shm: shm_00 =
shared_memory.SharedMemory(name='shm_object_00',create=False).
I have used shared memory at other points in thi
I started this post on November 29, and there have been helpful comments since
then from Barry Scott, Cameron Simpson, Peter Holzer and Chris Angelico.
Thanks to all of you.
I've found a solution that works for my purpose, and I said earlier that I
would post the solution I found. If anyone
@barrys-emacs.org:
>
>
>> On 6 Dec 2021, at 17:09, Jen Kris via Python-list
>> wrote:
>>
>> I can't find any support for your comment that "Fork creates a new
>> process and therefore also a new thread." From the Linux man pages
>> htt
t;).
You may be confused by the fact that threads are called light-weight processes.
Or maybe I'm confused :)
If you have other information, please let me know. Thanks.
Jen
Dec 5, 2021, 18:08 by hjp-pyt...@hjp.at:
> On 2021-12-06 00:51:13 +0100, Jen Kris via Python-list wrote:
>
gt;>
>>> Barry
>>>
>>>
>>>
>>>>
>>>>
>>>>
>>>> Nov 30, 2021, 11:42 by >>>> ba...@barrys-emacs.org>>>> :
>>>>
>>>>>
>>>>>
>>>>>
>>>>>> On 29 Nov 202
e opened as rdwr in Python because that's
>>>> nonblocking by default. The child will become more complex, but not in a
>>>> way that affects polling. And thanks for the tip about the c-string
>>>> termination.
>>>>
>>>>
>&g
useful to understand in detail what is behind os.open().
>
> Barry
>
>
>
>
>>
>>
>> Nov 29, 2021, 14:12 by >> ba...@barrys-emacs.org>> :
>>
>>>
>>>
>>>> On 29 Nov 2021, at 20:36, Jen Kris via Python-list <>>&g
hat's nonblocking by default.
The child will become more complex, but not in a way that affects polling. And
thanks for the tip about the c-string termination.
Nov 29, 2021, 14:12 by ba...@barrys-emacs.org:
>
>
>> On 29 Nov 2021, at 20:36, Jen Kris via Python-list
>&
I have a C program that forks to create a child process and uses execv to call
a Python program. The Python program communicates with the parent process (in
C) through a FIFO pipe monitored with epoll().
The Python child process is in a while True loop, which is intended to keep it
running w
67 matches
Mail list logo