Re: Best way to check if there is internet?

2022-02-09 Thread Gisle Vanem

Abdur-Rahmaan Janhangeer wrote:


Thanks everybody for the answers. It was very enlightening. Here's my
solution:

# using rich console
def ensure_internet():
console = Console()
domains = [
'https://google.com',
'https://yahoo.com',
'https://bing.com',
'https://www.ecosia.org',
'https://www.wikipedia.org'
]
results = []
with console.status("Checking internet ...", spinner="dots"):
for domain in domains:
try:
requests.get(domain)
results.append(1)
except Exception as e:
results.append(0)
if not any(results):
print('No internet connection')
sys.exit()


Was this supposed to be a self-contained working
program? It doesn't work here.

Were/what is 'Console()' for example?

--
--gv
--
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-09 Thread Abdur-Rahmaan Janhangeer
It's a demo to get the idea behind, uses requests and the rich library.

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius


On Wed, Feb 9, 2022 at 12:24 PM Gisle Vanem  wrote:

> Abdur-Rahmaan Janhangeer wrote:
>
> > Thanks everybody for the answers. It was very enlightening. Here's my
> > solution:
> >
> > # using rich console
> > def ensure_internet():
> > console = Console()
> > domains = [
> > 'https://google.com',
> > 'https://yahoo.com',
> > 'https://bing.com',
> > 'https://www.ecosia.org',
> > 'https://www.wikipedia.org'
> > ]
> > results = []
> > with console.status("Checking internet ...", spinner="dots"):
> > for domain in domains:
> > try:
> > requests.get(domain)
> > results.append(1)
> > except Exception as e:
> > results.append(0)
> > if not any(results):
> > print('No internet connection')
> > sys.exit()
>
> Was this supposed to be a self-contained working
> program? It doesn't work here.
>
> Were/what is 'Console()' for example?
>
> --
> --gv
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-09 Thread Gisle Vanem

Abdur-Rahmaan Janhangeer wrote:


It's a demo to get the idea behind, uses requests and the rich library.


I'd never heard of 'Rich'. But d/l it and playing
with it, I came up with this version with fat green
spinner-bar:

import requests
from rich.console import Console

def ensure_internet():
console = Console()
domains = [ "https://google.com";,
"https://yahoo.com";,
"https://bing.com";,
"https://www.ecosia.org";,
"https://www.wikipedia.org"; ]
results = []
with console.status ("Checking internet ...", spinner="material") as c:
for domain in domains:
c._spinner.text = "Checking %-40s" % domain
try:
requests.get(domain)
results.append(1)
except Exception as e:
results.append(0)

if not any(results):
   print("No internet connection")

ensure_internet()

---

Rich' is IMO a pretty amazing package.

--gv
--
https://mail.python.org/mailman/listinfo/python-list


Unpacking lists in a f string

2022-02-09 Thread Paulo da Silva

Hi!

Let's say I have two lists of equal length but with a variable number of 
elements. For ex.:


l1=['a','b','c']
l2=['j','k','l']

I want to build a string like this
"foo a j, b k, c l bar"

Is it possible to achieve this with f strings or any other 
simple/efficient way?


Thanks for any help/comments.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Unpacking lists in a f string

2022-02-09 Thread Python

Paulo da Silva wrote:

Hi!

Let's say I have two lists of equal length but with a variable number of 
elements. For ex.:


l1=['a','b','c']
l2=['j','k','l']

I want to build a string like this
"foo a j, b k, c l bar"

Is it possible to achieve this with f strings or any other 
simple/efficient way?


'foo ' + ', '.join(f"{one} {two}" for one,two in zip(l1,l2)) + ' bar'

--
https://mail.python.org/mailman/listinfo/python-list


PermissionError: [Errno 13] Permission denied: 'Abc.xlsx'

2022-02-09 Thread NArshad
When I enter data using Tkinter form in an Excel file when the excel file is 
closed there is no error but when I enter data using Tkinter form when the 
excel is already open following error comes:



Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Dani Brothers\Anaconda3\lib\tkinter\__init__.py", line 1705, 
in __call__
return self.func(*args)
  File "D:/Python/Book Bank/New folder/PyCharm/Final/Excel.py", line 61, in 
SaveBook
workbook.save(filename="BookBank.xlsx")
  File "C:\Users\Dani 
Brothers\Anaconda3\lib\site-packages\openpyxl\workbook\workbook.py", line 392, 
in save
save_workbook(self, filename)
  File "C:\Users\Dani 
Brothers\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 291, in 
save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
  File "C:\Users\Dani Brothers\Anaconda3\lib\zipfile.py", line 1207, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'Abc.xlsx'



What to do to correct this error? I have already searched on google search many 
times but no solution was found.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PermissionError: [Errno 13] Permission denied: 'Abc.xlsx'

2022-02-09 Thread Christian Gollwitzer

Am 09.02.22 um 08:46 schrieb NArshad:

When I enter data using Tkinter form in an Excel file when the excel file is 
closed there is no error but when I enter data using Tkinter form when the 
excel is already open following error comes:



PermissionError: [Errno 13] Permission denied: 'Abc.xlsx'



What to do to correct this error? I have already searched on google search many 
times but no solution was found.


It's impossible. Excel locks the file deliberately when it is open, so 
that you can't overwrite it from a different program. Otherwise, the 
file could become inconsistent.


The correct way to handle it in the GUI is to tell the user via a 
message box that the file is open and can't be written.


An alternative to writing the file directly would be that you remote 
control Excel; I think it provides a DDE API: 
https://support.microsoft.com/en-us/office/dde-function-79e8b21c-2054-4b48-9ceb-d2cf38dc17f9


Christian
--
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-09 Thread Akkana Peck
Abdur-Rahmaan Janhangeer writes:
> results = []
> with console.status("Checking internet ...", spinner="dots"):
> for domain in domains:
> try:
> requests.get(domain)
> results.append(1)
> except Exception as e:
> results.append(0)
> if not any(results):
> print('No internet connection')
> sys.exit()
> 
> gist link:
> https://gist.github.com/Abdur-rahmaanJ/7917dc5ab7f5d2aa37b2723909be08f7
> 
> I think for me having the internet means ability to request urls

Request urls and get the right answer?

This won't work if you're behind a captive portal: every URL you
try to get will return successfully, but the content will be
the captive portal page.

You need to compare the output of at least one of those pages to
known output to be sure you're really connected.

...Akkana
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can't get iterator in the C API

2022-02-09 Thread Jen Kris via Python-list
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 
>> 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)
>>      etc
>>
>> where gutenberg.fileids is, of course, iterable.
>>
>> I use the following C API code to import the module and get pointers:
>>
>> int64_t Call_PyModule()
>> {
>>      PyObject *pModule, *pName, *pSubMod, *pFidMod, *pFidSeqIter,*pSentMod;
>>
>>      pName = PyUnicode_FromString("nltk.corpus");
>>      pModule = PyImport_Import(pName);
>>
>>      if (pModule == 0x0){
>>      PyErr_Print();
>>      return 1; }
>>
>>      pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
>>      pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
>>      pSentMod = PyObject_GetAttrString(pSubMod, "sents");
>>
>>      pFidIter = PyObject_GetIter(pFidMod);
>>      int ckseq_ok = PySeqIter_Check(pFidMod);
>>      pFidSeqIter  = PySeqIter_New(pFidMod);
>>
>>      return 0;
>> }
>>
>> pSubMod, pFidMod and pSentMod all return valid pointers, but the iterator 
>> lines return zero:
>>
>> pFidIter = PyObject_GetIter(pFidMod);
>> int ckseq_ok = PySeqIter_Check(pFidMod);
>> pFidSeqIter  = PySeqIter_New(pFidMod);
>>
>> So the C API thinks gutenberg.fileids is not iterable, but it is.  What am I 
>> doing wrong?
>>
> Look at your Python code. You have "gutenberg.fileids()", so the 'fileids' 
> attribute is not an iterable itself, but a method that you need to call to 
> get the iterable.
> -- 
> https://mail.python.org/mailman/listinfo/python-list
>

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PermissionError: [Errno 13] Permission denied: 'Abc.xlsx'

2022-02-09 Thread jkn
On Wednesday, February 9, 2022 at 12:46:49 PM UTC, Christian Gollwitzer wrote:
> Am 09.02.22 um 08:46 schrieb NArshad:
> > When I enter data using Tkinter form in an Excel file when the excel file 
> > is closed there is no error but when I enter data using Tkinter form when 
> > the excel is already open following error comes:
> > PermissionError: [Errno 13] Permission denied: 'Abc.xlsx' 
> > 
> > 
> > 
> > What to do to correct this error? I have already searched on google search 
> > many times but no solution was found.
> It's impossible. Excel locks the file deliberately when it is open, so 
> that you can't overwrite it from a different program. Otherwise, the 
> file could become inconsistent. 
> 
> The correct way to handle it in the GUI is to tell the user via a 
> message box that the file is open and can't be written. 
> 
> An alternative to writing the file directly would be that you remote 
> control Excel; I think it provides a DDE API: 
> https://support.microsoft.com/en-us/office/dde-function-79e8b21c-2054-4b48-9ceb-d2cf38dc17f9
>  
> 
DDE? Wow, that takes me back...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unpacking lists in a f string

2022-02-09 Thread David Lowry-Duda
> l1=['a','b','c']
> l2=['j','k','l']
> 
> I want to build a string like this
> "foo a j, b k, c l bar"
> Is it possible to achieve this with f strings or any other 
> simple/efficient way?

Here is a small list of things that want to be done (and natural ways to 
perform them)

1. pair items in the lists (using list comprehension),
2. format the pairs (using an f-string),
3. separate the pairs with commas (using join), and
4. incorporate surrounding "foo" "bar" text. (using an f-string)

It would be possible to have

print(f"foo {', '.join(f'{first} {second}' for first, second in zip(l1, 
l2))} bar")

But I find nested f-strings to be confusing, and avoid them. This is a 
case where I actually prefer format.

print("foo {} bar".format(
', '.join(f'{first} {second}' for first, second in zip(l1, l2))
))

It's still a one-liner, but it's a one liner that I find easier to 
parse. Or perhaps I would explicitly construct the string first and then 
use it?

innerstr = ', '.join(f'{first} {second} for first, second in zip(l1, l2))
print(f"foo {innerstr} bar")

These ideas are what feel natural to me.

- DLD
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-09 Thread Abdur-Rahmaan Janhangeer
>  Ah, but WHEN do those browsers report that? When attempting to connect
to whatever the default "home" page has been set to? (Mine is configured to
use https://www.google.com as the default page -- if my router is down,
obviously the browser will time-out waiting for a response from Google, and
report "no network").

Yes you are right ... lol i misread the error i guess

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-09 Thread Abdur-Rahmaan Janhangeer
> This won't work if you're behind a captive portal: every URL you
try to get will return successfully, but the content will be
the captive portal page.

Yes agree a pretty common scenario if you are moving around ...
The solution is to test against a webpage you know the content
will always be the same. Thanks for pointing out!

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Correct way to setup a package with both compiled C code and Python code?

2022-02-09 Thread Dieter Maurer
Christian Gollwitzer wrote at 2022-2-8 22:43 +0100:
>Am 08.02.22 um 18:57 schrieb Dieter Maurer:
>> Christian Gollwitzer wrote at 2022-2-7 20:33 +0100:
>>> we've developed a Python pacakge which consists of both a compiled
>>> extension module and some helper functions in Python. Is there a
>>> tutorial on how to package such an extension?
>>
>> Look at "https://package.python.org";,
>> especially 
>> "https://packaging.python.org/en/latest/guides/packaging-binary-extensions/";.
>
>Thank you, but that page is more like a high-level description, it talks
>a lot about the differences between C code and Python and how it can be
>combined using SWIG etc, but I already have a working extension.

Packaging and distribution was originally handled by `distutils`;
other tools, e.g. `setuptools`, extended `distutils` and have
gained wide spread acceptance. As a consequence, `distutils`
has been deprecated in favor of those other tools.
I assume the use of `setuptools` below.

Extensions are described via the `ext_modules` parameter of
the `setup` function. Its value is a sequence of
`setuptools.Extension` instances.

As of Python 3.11, `setuptools.Extension` is still a minor enhancement
of `distutils.extension.Extension`. This is documented via
docstrings in its source.
Read this documentation and come back if questions remain.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PermissionError: [Errno 13] Permission denied: 'Abc.xlsx'

2022-02-09 Thread MRAB

On 2022-02-09 12:45, Christian Gollwitzer wrote:

Am 09.02.22 um 08:46 schrieb NArshad:

When I enter data using Tkinter form in an Excel file when the excel file is 
closed there is no error but when I enter data using Tkinter form when the 
excel is already open following error comes:



PermissionError: [Errno 13] Permission denied: 'Abc.xlsx'



What to do to correct this error? I have already searched on google search many 
times but no solution was found.


It's impossible. Excel locks the file deliberately when it is open, so
that you can't overwrite it from a different program. Otherwise, the
file could become inconsistent.

It's the same the other way too; you can't open the file in Excel while 
Python has it open.



The correct way to handle it in the GUI is to tell the user via a
message box that the file is open and can't be written.

An alternative to writing the file directly would be that you remote
control Excel; I think it provides a DDE API:
https://support.microsoft.com/en-us/office/dde-function-79e8b21c-2054-4b48-9ceb-d2cf38dc17f9


--
https://mail.python.org/mailman/listinfo/python-list


Re: Unpacking lists in a f string

2022-02-09 Thread Paulo da Silva

Às 02:17 de 09/02/22, Paulo da Silva escreveu:

Hi!

Let's say I have two lists of equal length but with a variable number of 
elements. For ex.:


l1=['a','b','c']
l2=['j','k','l']

I want to build a string like this
"foo a j, b k, c l bar"

Is it possible to achieve this with f strings or any other 
simple/efficient way?


Thanks for any help/comments.


Thank you for your responses.
--
https://mail.python.org/mailman/listinfo/python-list


Re: PermissionError: [Errno 13] Permission denied: 'Abc.xlsx'

2022-02-09 Thread Dennis Lee Bieber
On Tue, 8 Feb 2022 23:46:15 -0800 (PST), NArshad 
declaimed the following:

>When I enter data using Tkinter form in an Excel file when the excel file is 
>closed there is no error but when I enter data using Tkinter form when the 
>excel is already open following error comes:
>


>PermissionError: [Errno 13] Permission denied: 'Abc.xlsx'
>

>What to do to correct this error? I have already searched on google search 
>many times but no solution was found.

Well -- at the basic level... Make sure that only ONE user/application
has access to the file at any given moment.

Excel (and spreadsheets opened by it) are single-user applications for
a reason -- to prevent the data in the file from being changed without the
application knowing about it. They are not databases designed to allow
multiple users to make changes.

YOU will have to implement transaction control over the file. At the
rudimentary level (since you can't change how Excel itself operates)
whenever you intend to save changes to the data you have to notify any user
that has the file open that they need to close/save the file; when that has
been done you have to reread the file (because they may have made changes
to the contents that you haven't seen yet), reapply any of your changes (if
still valid conditions), then save the file. You can then notify the other
users that the file is updated and can be opened by them. If you don't
reread/validate the data before applying your changes, you would wipe out
any changes made by others.

Now, if you have total control over the applications that will access
the file (which essentially means: NOBODY will use Excel itself) you could
write a client/server scheme. In this scheme you would have one process
(the server) as the only program that does anything with the spreadsheet
file and its contents. Your client programs would connect (TCP sockets most
likely) to the server process and send it "commands" (something to be
defined/documented is an interface control document); the server would
parse the commands and implement changes to the file data, and/or return
any requested data to the client. That, at least avoids the file level
conflicts. You still have to figure out how to handle the case where two
clients try to update the same record (record level locking). 

One possibility is to have every record contain a time-stamp of the
last change -- and I'm going to assume the "commands" are record based, not
cell based -- and the command protocol for update sends back the time-stamp
originally read; the server code would compare the time-stamp with what
that record has in the file -- if they are the same, update the entire
record including a new time-stamp; if different, return a "conflict" status
with the current state of the record (with time-stamp) -- the client can
then compare the new record with the stale one, make any changes, and retry
the update. NOTE: this takes care of single record conflicts, but will help
if a "transaction" has to update multiple records since there is no history
to allow "all or none succeed" logic to be implemented.

"Commands"
READREC 
returns status (no such record) and (if valid) 
specified
record including last time-stamp
WRITEREC  
returns status (update conflict and current 
record contents
with time-stamp); if recno is unused, can pass 0 for timestamp to write new
record; upon write the time-stamp is updated
UPDATE
basically same as WRITEREC
FIND  
returns list of recno for records matching the 
search (not
the records themselves)


A proper client/server database handles most of the problem of keeping
the data (file) uncorrupted... Though clients attempting to update the same
record will have one succeed, the other get an exception -- which would be
handled by: rereading the record(s) one attempted to update, confirming the
conditions for the update are still valid, and reapplying the update.
Multiple record updates within a transaction are possible.


-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you log in your projects?

2022-02-09 Thread Martin Di Paola

- On a line per line basis? on a function/method basis?


In general I prefer logging line by line instead per function.

It is easy to add a bunch of decorators to the functions and get the 
logs of all the program but I most of the time I end up with very 
confusing logs.


There are exceptions, yes, but I prefer the line by line where the log 
should explain what is doing the code.



- Which kind of variable contents do you write into your logfiles?
- How do you decide, which kind of log message goes into which level?
- How do you prevent logging cluttering your actual code?


These three comes to the same answer: I think on whom is going to read 
the logs.


If the logs are meant to be read by my users I log high level messages,
specially before parts that can take a while (like the classic 
"Loading...").


If I log variables, those must be the ones set by the users so he/she 
can understand how he/she is controlling the behaviour of the program.


For exceptions I print the message but not the traceback. Across the 
code tag some important functions to put an extra message that will 
enhance the final message printed to the user.


https://github.com/byexamples/byexample/blob/master/byexample/common.py#L192-L238

For example:

for example in examples:
with enhance_exceptions(example, ...):
foo()

So if an exception is raised by foo(), enhance_exceptions() will attach 
to it useful information for the user from the example variable.


In the main, then I do the pretty print
https://github.com/byexamples/byexample/blob/master/byexample/byexample.py#L17-L22

If the user of the logs is me or any other developer I write more debugging 
stuff.

My approach is to not log anything and when I have to debug something 
I use a debugger + some prints. When the issue is fixed I review which 
prints would be super useful and I turn them into logs and the rest is 
deleted.



On Tue, Feb 08, 2022 at 09:40:07PM +0100, Marco Sulla wrote:

These are a lot of questions. I hope we're not off topic.
I don't know if mine are best practices. I can tell what I try to do.

On Tue, 8 Feb 2022 at 15:15, Lars Liedtke  wrote:

- On a line per line basis? on a function/method basis?


I usually log the start and end of functions. I could also log inside
a branch or in other parts of the function/method.


- Do you use decorators to mark beginnings and ends of methods/functions
in log files?


No, since I put the function parameters in the first log. But I think
that such a decorator it's not bad.


- Which kind of variable contents do you write into your logfiles? Of
course you shouldn't leak secrets...


Well, all the data that is useful to understand what the code is
doing. It's better to repeat the essential data to identify a specific
call in all the logs of the function, so if it is called
simultaneously by more clients you can distinguish them


- How do you decide, which kind of log message goes into which level?


It depends on the importance, the verbosity and the occurrences of the logs.


- How do you prevent logging cluttering your actual code?


I have the opposite problem, I should log more. So I can't answer your question.
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list


Re: Unpacking lists in a f string

2022-02-09 Thread Kirill Ratkin

Hi.

Try this:

f"foo {','.join([f'{a} {b}' for a,b in list(zip(l1,l2))])} bar"

09.02.2022 21:13, Paulo da Silva пишет:

Às 02:17 de 09/02/22, Paulo da Silva escreveu:

Hi!

Let's say I have two lists of equal length but with a variable number 
of elements. For ex.:


l1=['a','b','c']
l2=['j','k','l']

I want to build a string like this
"foo a j, b k, c l bar"

Is it possible to achieve this with f strings or any other 
simple/efficient way?


Thanks for any help/comments.


Thank you for your responses.

--
https://mail.python.org/mailman/listinfo/python-list


C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
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 enumerate(gutenberg.fileids()):
    sentences = gutenberg.sents(fileid)
    etc

I have everything finished down to the last line (sentences = 
gutenberg.sents(fileid)) where I use  PyObject_Call to call gutenberg.sents, 
but it segfaults.  The fileid is a string -- the first fileid in this corpus is 
"austen-emma.txt."  

pName = PyUnicode_FromString("nltk.corpus");
pModule = PyImport_Import(pName);

pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
pSentMod = PyObject_GetAttrString(pSubMod, "sents");

pFileIds = PyObject_CallObject(pFidMod, 0);
pListItem = PyList_GetItem(pFileIds, listIndex);
pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
pListStr = PyBytes_AS_STRING(pListStrE);
Py_DECREF(pListStrE);

// sentences = gutenberg.sents(fileid)
PyObject *c_args = Py_BuildValue("s", pListStr);  
PyObject *NullPtr = 0;
pSents = PyObject_Call(pSentMod, c_args, NullPtr);

The final line segfaults:
Program received signal SIGSEGV, Segmentation fault.
0x76e4e8d5 in _PyEval_EvalCodeWithName ()
   from /usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0

My guess is the problem is in Py_BuildValue, which returns a pointer but it may 
not be constructed correctly.  I also tried it with "O" and it doesn't segfault 
but it returns 0x0. 

I'm new to using the C API.  Thanks for any help. 

Jen


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Inada Naoki
On Thu, Feb 10, 2022 at 9:42 AM Jen Kris via Python-list
 wrote:
>
> I have everything finished down to the last line (sentences = 
> gutenberg.sents(fileid)) where I use  PyObject_Call to call gutenberg.sents, 
> but it segfaults.  The fileid is a string -- the first fileid in this corpus 
> is "austen-emma.txt."
>
> pName = PyUnicode_FromString("nltk.corpus");
> pModule = PyImport_Import(pName);
>
> pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
> pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
> pSentMod = PyObject_GetAttrString(pSubMod, "sents");
>
> pFileIds = PyObject_CallObject(pFidMod, 0);
> pListItem = PyList_GetItem(pFileIds, listIndex);
> pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
> pListStr = PyBytes_AS_STRING(pListStrE);
> Py_DECREF(pListStrE);

HERE.
PyBytes_AS_STRING() returns pointer in the pListStrE Object.
So Py_DECREF(pListStrE) makes pListStr a dangling pointer.

>
> // sentences = gutenberg.sents(fileid)
> PyObject *c_args = Py_BuildValue("s", pListStr);

Why do you encode&decode pListStrE?
Why don't you use just pListStrE?

> PyObject *NullPtr = 0;
> pSents = PyObject_Call(pSentMod, c_args, NullPtr);
>

c_args must tuple, but you passed a unicode object here.
Read https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue


> The final line segfaults:
> Program received signal SIGSEGV, Segmentation fault.
> 0x76e4e8d5 in _PyEval_EvalCodeWithName ()
>from /usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0
>
> My guess is the problem is in Py_BuildValue, which returns a pointer but it 
> may not be constructed correctly.  I also tried it with "O" and it doesn't 
> segfault but it returns 0x0.
>
> I'm new to using the C API.  Thanks for any help.
>
> Jen
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Bests,

-- 
Inada Naoki  
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
Thanks for your reply.  

I eliminated the DECREF and now it doesn't segfault but it returns 0x0.  Same 
when I substitute pListStrE for pListStr.  pListStr contains the string 
representation of the fileid, so it seemed like the one to use.  According to  
http://web.mit.edu/people/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. 


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 everything finished down to the last line (sentences = 
>> gutenberg.sents(fileid)) where I use  PyObject_Call to call gutenberg.sents, 
>> but it segfaults.  The fileid is a string -- the first fileid in this corpus 
>> is "austen-emma.txt."
>>
>> pName = PyUnicode_FromString("nltk.corpus");
>> pModule = PyImport_Import(pName);
>>
>> pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
>> pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
>> pSentMod = PyObject_GetAttrString(pSubMod, "sents");
>>
>> pFileIds = PyObject_CallObject(pFidMod, 0);
>> pListItem = PyList_GetItem(pFileIds, listIndex);
>> pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
>> pListStr = PyBytes_AS_STRING(pListStrE);
>> Py_DECREF(pListStrE);
>>
>
> HERE.
> PyBytes_AS_STRING() returns pointer in the pListStrE Object.
> So Py_DECREF(pListStrE) makes pListStr a dangling pointer.
>
>>
>> // sentences = gutenberg.sents(fileid)
>> PyObject *c_args = Py_BuildValue("s", pListStr);
>>
>
> Why do you encode&decode pListStrE?
> Why don't you use just pListStrE?
>
>> PyObject *NullPtr = 0;
>> pSents = PyObject_Call(pSentMod, c_args, NullPtr);
>>
>
> c_args must tuple, but you passed a unicode object here.
> Read https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue
>
>
>> The final line segfaults:
>> Program received signal SIGSEGV, Segmentation fault.
>> 0x76e4e8d5 in _PyEval_EvalCodeWithName ()
>>  from /usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0
>>
>> My guess is the problem is in Py_BuildValue, which returns a pointer but it 
>> may not be constructed correctly.  I also tried it with "O" and it doesn't 
>> segfault but it returns 0x0.
>>
>> I'm new to using the C API.  Thanks for any help.
>>
>> Jen
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
> Bests,
>
> -- 
> Inada Naoki  
>

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Inada Naoki
On Thu, Feb 10, 2022 at 10:05 AM Jen Kris  wrote:
>
> Thanks for your reply.
>
> I eliminated the DECREF and now it doesn't segfault but it returns 0x0.  Same 
> when I substitute pListStrE for pListStr.  pListStr contains the string 
> representation of the fileid, so it seemed like the one to use.  According to 
>  http://web.mit.edu/people/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.


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 everything finished down to the last line (sentences = 
> gutenberg.sents(fileid)) where I use PyObject_Call to call gutenberg.sents, 
> but it segfaults. The fileid is a string -- the first fileid in this corpus 
> is "austen-emma.txt."
>
> pName = PyUnicode_FromString("nltk.corpus");
> pModule = PyImport_Import(pName);
>
> pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
> pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
> pSentMod = PyObject_GetAttrString(pSubMod, "sents");
>
> pFileIds = PyObject_CallObject(pFidMod, 0);
> pListItem = PyList_GetItem(pFileIds, listIndex);
> pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
> pListStr = PyBytes_AS_STRING(pListStrE);
> Py_DECREF(pListStrE);
>
>
> HERE.
> PyBytes_AS_STRING() returns pointer in the pListStrE Object.
> So Py_DECREF(pListStrE) makes pListStr a dangling pointer.
>
>
> // sentences = gutenberg.sents(fileid)
> PyObject *c_args = Py_BuildValue("s", pListStr);
>
>
> Why do you encode&decode pListStrE?
> Why don't you use just pListStrE?
>
> PyObject *NullPtr = 0;
> pSents = PyObject_Call(pSentMod, c_args, NullPtr);
>
>
> c_args must tuple, but you passed a unicode object here.
> Read https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue
>
> The final line segfaults:
> Program received signal SIGSEGV, Segmentation fault.
> 0x76e4e8d5 in _PyEval_EvalCodeWithName ()
> from /usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0
>
> My guess is the problem is in Py_BuildValue, which returns a pointer but it 
> may not be constructed correctly. I also tried it with "O" and it doesn't 
> segfault but it returns 0x0.
>
> I'm new to using the C API. Thanks for any help.
>
> Jen
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>
> Bests,
>
> --
> Inada Naoki 
>
>


-- 
Inada Naoki  
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
Right you are.  In that case should I use Py_BuildValue and convert to tuple 
(because it won't return a tuple for a one-arg), or should I just convert 
pListStr to tuple?  Thanks for your help.  


Feb 9, 2022, 17:08 by songofaca...@gmail.com:

> On Thu, Feb 10, 2022 at 10:05 AM Jen Kris  wrote:
>
>>
>> Thanks for your reply.
>>
>> I eliminated the DECREF and now it doesn't segfault but it returns 0x0.  
>> Same when I substitute pListStrE for pListStr.  pListStr contains the string 
>> representation of the fileid, so it seemed like the one to use.  According 
>> to  http://web.mit.edu/people/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.
>
>
> 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 everything finished down to the last line (sentences = 
>> gutenberg.sents(fileid)) where I use PyObject_Call to call gutenberg.sents, 
>> but it segfaults. The fileid is a string -- the first fileid in this corpus 
>> is "austen-emma.txt."
>>
>> pName = PyUnicode_FromString("nltk.corpus");
>> pModule = PyImport_Import(pName);
>>
>> pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
>> pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
>> pSentMod = PyObject_GetAttrString(pSubMod, "sents");
>>
>> pFileIds = PyObject_CallObject(pFidMod, 0);
>> pListItem = PyList_GetItem(pFileIds, listIndex);
>> pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
>> pListStr = PyBytes_AS_STRING(pListStrE);
>> Py_DECREF(pListStrE);
>>
>>
>> HERE.
>> PyBytes_AS_STRING() returns pointer in the pListStrE Object.
>> So Py_DECREF(pListStrE) makes pListStr a dangling pointer.
>>
>>
>> // sentences = gutenberg.sents(fileid)
>> PyObject *c_args = Py_BuildValue("s", pListStr);
>>
>>
>> Why do you encode&decode pListStrE?
>> Why don't you use just pListStrE?
>>
>> PyObject *NullPtr = 0;
>> pSents = PyObject_Call(pSentMod, c_args, NullPtr);
>>
>>
>> c_args must tuple, but you passed a unicode object here.
>> Read https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue
>>
>> The final line segfaults:
>> Program received signal SIGSEGV, Segmentation fault.
>> 0x76e4e8d5 in _PyEval_EvalCodeWithName ()
>> from /usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0
>>
>> My guess is the problem is in Py_BuildValue, which returns a pointer but it 
>> may not be constructed correctly. I also tried it with "O" and it doesn't 
>> segfault but it returns 0x0.
>>
>> I'm new to using the C API. Thanks for any help.
>>
>> Jen
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>>
>> Bests,
>>
>> --
>> Inada Naoki 
>>
>
>
> -- 
> Inada Naoki  
>

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Inada Naoki
// https://docs.python.org/3/c-api/call.html#c.PyObject_CallNoArgs
// This function is only for one arg. Python >= 3.9 is required.
pSents = PyObject_CallOneArg(pSentMod, pListItem);

Or

// https://docs.python.org/3/c-api/call.html#c.PyObject_CallFunctionObjArgs
// This function can call function with multiple arguments. Can be
used with Python <3.9 too.
pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem);

On Thu, Feb 10, 2022 at 10:15 AM Jen Kris  wrote:
>
> Right you are.  In that case should I use Py_BuildValue and convert to tuple 
> (because it won't return a tuple for a one-arg), or should I just convert 
> pListStr to tuple?  Thanks for your help.
>
>
> Feb 9, 2022, 17:08 by songofaca...@gmail.com:
>
> On Thu, Feb 10, 2022 at 10:05 AM Jen Kris  wrote:
>
>
> Thanks for your reply.
>
> I eliminated the DECREF and now it doesn't segfault but it returns 0x0. Same 
> when I substitute pListStrE for pListStr. pListStr contains the string 
> representation of the fileid, so it seemed like the one to use. According to 
> http://web.mit.edu/people/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.
>
>
> 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 everything finished down to the last line (sentences = 
> gutenberg.sents(fileid)) where I use PyObject_Call to call gutenberg.sents, 
> but it segfaults. The fileid is a string -- the first fileid in this corpus 
> is "austen-emma.txt."
>
> pName = PyUnicode_FromString("nltk.corpus");
> pModule = PyImport_Import(pName);
>
> pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
> pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
> pSentMod = PyObject_GetAttrString(pSubMod, "sents");
>
> pFileIds = PyObject_CallObject(pFidMod, 0);
> pListItem = PyList_GetItem(pFileIds, listIndex);
> pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
> pListStr = PyBytes_AS_STRING(pListStrE);
> Py_DECREF(pListStrE);
>
>
> HERE.
> PyBytes_AS_STRING() returns pointer in the pListStrE Object.
> So Py_DECREF(pListStrE) makes pListStr a dangling pointer.
>
>
> // sentences = gutenberg.sents(fileid)
> PyObject *c_args = Py_BuildValue("s", pListStr);
>
>
> Why do you encode&decode pListStrE?
> Why don't you use just pListStrE?
>
> PyObject *NullPtr = 0;
> pSents = PyObject_Call(pSentMod, c_args, NullPtr);
>
>
> c_args must tuple, but you passed a unicode object here.
> Read https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue
>
> The final line segfaults:
> Program received signal SIGSEGV, Segmentation fault.
> 0x76e4e8d5 in _PyEval_EvalCodeWithName ()
> from /usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0
>
> My guess is the problem is in Py_BuildValue, which returns a pointer but it 
> may not be constructed correctly. I also tried it with "O" and it doesn't 
> segfault but it returns 0x0.
>
> I'm new to using the C API. Thanks for any help.
>
> Jen
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>
> Bests,
>
> --
> Inada Naoki 
>
>
>
> --
> Inada Naoki 
>
>


-- 
Inada Naoki  
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
I'm using Python 3.8 so I tried your second choice:

pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem);

but pSents is 0x0.  pSentMod and pListItem are valid pointers.  


Feb 9, 2022, 17:23 by songofaca...@gmail.com:

> // https://docs.python.org/3/c-api/call.html#c.PyObject_CallNoArgs
> // This function is only for one arg. Python >= 3.9 is required.
> pSents = PyObject_CallOneArg(pSentMod, pListItem);
>
> Or
>
> // https://docs.python.org/3/c-api/call.html#c.PyObject_CallFunctionObjArgs
> // This function can call function with multiple arguments. Can be
> used with Python <3.9 too.
> pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem);
>
> On Thu, Feb 10, 2022 at 10:15 AM Jen Kris  wrote:
>
>>
>> Right you are.  In that case should I use Py_BuildValue and convert to tuple 
>> (because it won't return a tuple for a one-arg), or should I just convert 
>> pListStr to tuple?  Thanks for your help.
>>
>>
>> Feb 9, 2022, 17:08 by songofaca...@gmail.com:
>>
>> On Thu, Feb 10, 2022 at 10:05 AM Jen Kris  wrote:
>>
>>
>> Thanks for your reply.
>>
>> I eliminated the DECREF and now it doesn't segfault but it returns 0x0. Same 
>> when I substitute pListStrE for pListStr. pListStr contains the string 
>> representation of the fileid, so it seemed like the one to use. According to 
>> http://web.mit.edu/people/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.
>>
>>
>> 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 everything finished down to the last line (sentences = 
>> gutenberg.sents(fileid)) where I use PyObject_Call to call gutenberg.sents, 
>> but it segfaults. The fileid is a string -- the first fileid in this corpus 
>> is "austen-emma.txt."
>>
>> pName = PyUnicode_FromString("nltk.corpus");
>> pModule = PyImport_Import(pName);
>>
>> pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
>> pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
>> pSentMod = PyObject_GetAttrString(pSubMod, "sents");
>>
>> pFileIds = PyObject_CallObject(pFidMod, 0);
>> pListItem = PyList_GetItem(pFileIds, listIndex);
>> pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
>> pListStr = PyBytes_AS_STRING(pListStrE);
>> Py_DECREF(pListStrE);
>>
>>
>> HERE.
>> PyBytes_AS_STRING() returns pointer in the pListStrE Object.
>> So Py_DECREF(pListStrE) makes pListStr a dangling pointer.
>>
>>
>> // sentences = gutenberg.sents(fileid)
>> PyObject *c_args = Py_BuildValue("s", pListStr);
>>
>>
>> Why do you encode&decode pListStrE?
>> Why don't you use just pListStrE?
>>
>> PyObject *NullPtr = 0;
>> pSents = PyObject_Call(pSentMod, c_args, NullPtr);
>>
>>
>> c_args must tuple, but you passed a unicode object here.
>> Read https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue
>>
>> The final line segfaults:
>> Program received signal SIGSEGV, Segmentation fault.
>> 0x76e4e8d5 in _PyEval_EvalCodeWithName ()
>> from /usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0
>>
>> My guess is the problem is in Py_BuildValue, which returns a pointer but it 
>> may not be constructed correctly. I also tried it with "O" and it doesn't 
>> segfault but it returns 0x0.
>>
>> I'm new to using the C API. Thanks for any help.
>>
>> Jen
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>>
>> Bests,
>>
>> --
>> Inada Naoki 
>>
>>
>>
>> --
>> Inada Naoki 
>>
>
>
> -- 
> Inada Naoki  
>

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Inada Naoki
On Thu, Feb 10, 2022 at 10:37 AM Jen Kris  wrote:
>
> I'm using Python 3.8 so I tried your second choice:
>
> pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem);
>
> but pSents is 0x0.  pSentMod and pListItem are valid pointers.
>

It means exception happened.
If you are writing Python/C function, return NULL (e.g. `if (pSents ==
NULL) return NULL`)
Then Python show the exception and traceback for you.

-- 
Inada Naoki  
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-09 Thread Avi Gross via Python-list
Actually, you may want a dynamic page. Pages may sometimes be delivered from 
some cache along the way perhaps within your own company firewall if someone 
else recently accessed them.
Consider a page that returns the current time or like the following asks for an 
arithmetical calculation to add 5 and 6
https://search.yahoo.com/search?fr=mcafee&type=E211US1249G0&p=%3D5%2B6
Not a great example, and it may not work for you, but there likely is a site 
that can exist that accepts a request for some formatted calculation that you 
can specify dynamically. You can compare a date/time returned to being accurate 
within some seconds and asking for n*m can obviously be validated.
Of course, once such a ploy is well known, it can be intercepted and sent 
proper results and still fool you into assuming you had full internet access. 
So a site that you can send to and receive back using a cryptographic method 
might be better.



-Original Message-
From: Abdur-Rahmaan Janhangeer 
To: Python 
Sent: Wed, Feb 9, 2022 12:24 pm
Subject: Re: Best way to check if there is internet?

> This won't work if you're behind a captive portal: every URL you
try to get will return successfully, but the content will be
the captive portal page.

Yes agree a pretty common scenario if you are moving around ...
The solution is to test against a webpage you know the content
will always be the same. Thanks for pointing out!

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
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:
>>
>> pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem);
>>
>> but pSents is 0x0.  pSentMod and pListItem are valid pointers.
>>
>
> It means exception happened.
> If you are writing Python/C function, return NULL (e.g. `if (pSents ==
> NULL) return NULL`)
> Then Python show the exception and traceback for you.
>
> -- 
> Inada Naoki  
>

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-09 Thread MRAB

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);

but pSents is 0x0.  pSentMod and pListItem are valid pointers.


'PyObject_CallFunction' looks like a good one to use:

"""PyObject* PyObject_CallFunction(PyObject *callable, const char 
*format, ...)


Call a callable Python object callable, with a variable number of C 
arguments. The C arguments are described using a Py_BuildValue() style 
format string. The format can be NULL, indicating that no arguments are 
provided.

"""

[snip]

What I do is add comments to keep track of what objects I have 
references to at each point and whether they are new references or could 
be NULL.


For example:

pName = PyUnicode_FromString("nltk.corpus");
//> pName+?

This means that 'pName' contains a reference, '+' means that it's a new 
reference, and '?' means that it could be NULL (usually due to an 
exception, but not always) so I need to check it.


Continuing in this vein:

pModule = PyImport_Import(pName);
//> pName+? pModule+?

pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
//> pName+? pModule+? pSubMod+?
pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
//> pName+? pModule+? pSubMod+? pFidMod+?
pSentMod = PyObject_GetAttrString(pSubMod, "sents");
//> pName+? pModule+? pSubMod+? pFidMod+? pSentMod+?

pFileIds = PyObject_CallObject(pFidMod, 0);
//> pName+? pModule+? pSubMod+? pFidMod+? pSentMod+? 
PyObject_CallObject+?

pListItem = PyList_GetItem(pFileIds, listIndex);
//> pName+? pModule+? pSubMod+? pFidMod+? pSentMod+? 
PyObject_CallObject+? pListItem?

pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
//> pName+? pModule+? pSubMod+? pFidMod+? pSentMod+? 
PyObject_CallObject+? pListItem? pListStrE+?


As you can see, there's a lot of leaked references building up.

Note how after:

pListItem = PyList_GetItem(pFileIds, listIndex);

the addition is:

//> pListItem?

This means that 'pListItem' contains a borrowed (not new) reference, but 
could be NULL.


I find it easiest to DECREF as soon as I no longer need the reference 
and remove a name from the list as soon I no longer need it (and 
DECREFed where).


For example:

pName = PyUnicode_FromString("nltk.corpus");
//> pName+?
if (!pName)
goto error;
//> pName+
pModule = PyImport_Import(pName);
//> pName+ pModule+?
Py_DECREF(pName);
//> pModule+?
if (!pModule)
goto error;
//> pModule+

I find that doing this greatly reduces the chances of getting the 
reference counting wrong, and I can remove the comments once I've 
finished the function I'm writing.

--
https://mail.python.org/mailman/listinfo/python-list


How to solve the given problem?

2022-02-09 Thread NArshad


Assume that there is a pattern of feeding for a special fish in a day (10 hours 
a day) as below:
   150100303030202010   
 55
Today, the fish is fed in the second hour 60 unit instead of 100 unit 
Accidently. Implement some methods to distribute the remaining 40 unit in the 
rest of the day and propose the new patterns. Try to keep the distribution 
similar to the current feeding pattern. 
Note: pay attention that the total feeding amounts should be fix in a day.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to solve the given problem?

2022-02-09 Thread Christian Gollwitzer

Am 10.02.22 um 07:40 schrieb NArshad:


Assume that there is a pattern of feeding for a special fish in a day (10 hours 
a day) as below:
150100303030202010  
  55
Today, the fish is fed in the second hour 60 unit instead of 100 unit 
Accidently. Implement some methods to distribute the remaining 40 unit in the 
rest of the day and propose the new patterns. Try to keep the distribution 
similar to the current feeding pattern.
Note: pay attention that the total feeding amounts should be fix in a day.


This is not a Python problem, it's a math problem and most probably a 
homework problem. Actually the question already tells you how to solve 
it. There are 40 units of fish-food left and you should distribute them 
proportionally to the rest of the day. Sum up the numbers from 3rd to 
last, add 40, and then distribute this to proportionally to each day.


You'll end up with fractinoal numbers in the general case, so you'll 
have to find a method to fairly distribute the units, if you wan to 
stick with integers.


You can also check out the various algorithms for distributing seats in 
a parliament, it is almost the same problem.


Christian
--
https://mail.python.org/mailman/listinfo/python-list