Re: How to solve the given problem?

2022-02-10 Thread Chris Angelico
On Thu, 10 Feb 2022 at 18:41, NArshad  wrote:
>
>
> 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.

Once again, you're bringing your homework to a mailing list and
expecting us to do it for you.

Start by putting in some actual work yourself, and stop trying to
cheat your way to a good grade.

Please, can people not assist this person until there's some
demonstration of actual work being done?

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


Multiple inheritance using super() in parent classes

2022-02-10 Thread Igor Basko
Hi everyone,
This is my first question here. Hope to get some clarification.
Basically this question is about multiple inheritance and the usage of
super().__init__ in parent
classes.

So I have two classes that inherit from the same base class.
For example class B and class C inherit from A:
class A:
  def __init__(self, arg1):
pass

class B(A):
  def __init__(self, arg2):
super().__init__(arg2)

class C(A):
  def __init__(self, arg1, arg2):
super().__init__(arg2)

Now I would like to create a new class D that inherits from B and C.
One note, D is the only class that I am "allowed" to change. A, B and C are
provided to me as is from an external package.
class D(B, C):
  def __init__(self):
B.__init__(self, 'arg1')
C.__init__(self, 'arg1', 'arg2')

When I initialize D I get a TypeError.
TypeError: __init__() missing 1 required positional argument: 'arg2'
I get it from the invocation of super().__init__(arg2) inside the B class.

As I understand it, the super() inside B tries to call the __init__ of
class C,
because of the multiple inheritance and the MRO that is constructed.
But when B was implemented it wasn't aware of C and I assume,
B shouldn't be aware of C in any case.

It gives me the feeling that I'm trying to implement some bad practice
here, but I'm not sure why.

I would also like to hear your suggestions if there is a way to circumvent
it. Maybe by the approach described here:
https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
wrapping B and C in some Adapter class.

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


Global VS Local Subroutines

2022-02-10 Thread BlindAnagram

Is there any difference in performance between these two program layouts:

   def a():
 ...
   def(b):
 c = a(b)

or

   def(b):
 def a():
   ...
 c = a(b)

I would appreciate any insights on which layout to choose in which 
circumstances.


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


Re: How to solve the given problem?

2022-02-10 Thread NArshad
-ChrisA:
You don't reply if you have problems.
When I don't find any solution elsewhere then only I place in this group 


-Christian:
One problem of different type requires the same elaboration.

Q. What technique of statistics or numerical computation or general mathematics 
to use to solve this problem. Can you tell one example.

Find the values of 𝑥subscript(𝑖) for i from 1 to n (n =100).

𝑥subscript(1) + 𝑥subscript(2) = 3,

𝑥subscript(𝑖+1) − 𝑥subscript(𝑖+2) = 1, 𝑓𝑜𝑟 𝑖 = 1, … , 𝑛 − 2

𝑥subscript(n-1) + 𝑥subscript(n) = 3




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


Abstraction level at which to create SQLAlchemy ORM object

2022-02-10 Thread Loris Bennett
Hi,

I am writing a command line program which will modify entries in a
database and am trying out SQLAlchemy.

A typical command might look like

  um --operation add --uid ada --gid coders --lang en

Parsing the arguments I get, ignoring the operation, a dict

  {uid: "ada", gid: "coders", lang: "en"}

At some point this needs to be converted into an object of the class User:

  class User(Base):
  __tablename__ = "users"

  uid = Column('uid', String, primary_key=True)
  gid = Column('gid', String)
  lang = Column('lang', String)

In a way it seems it would be economical to do the conversion as early
as possible, so I can just pass around User objects.  However, this
would mean that the entry point for the program would already be tightly
coupled to the specifics of the database interaction.  

On the other hand, delaying the conversion would mean probably having to
define my own User class, which seems like unnecessary overhead.  It
would have the advantage that I could ditch SQLAlchemy more easily if I
find it too mind-bending.

WDYT?

Cheers,

Loris

-- 
This signature is currently under construction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Global VS Local Subroutines

2022-02-10 Thread Chris Angelico
On Fri, 11 Feb 2022 at 02:13, BlindAnagram  wrote:
>
> Is there any difference in performance between these two program layouts:
>
> def a():
>   ...
> def(b):
>   c = a(b)
>
> or
>
> def(b):
>   def a():
> ...
>   c = a(b)
>
> I would appreciate any insights on which layout to choose in which
> circumstances.
>

Great question! The difference is that, in the second case, a() isn't
available anywhere else. So the real question is: Is that good or bad?

Does it make sense to call a() from outside of your second function,
even for testing? If so, maybe name it _a() so it's private, but keep
it global. Does a() need a lot of information from the context of your
second function? If so, it's easier to have a closure, with one
function inside another.

Both styles make sense, and your question is well put: it's a design
decision with no "right" or "wrong", just different choices with
different implications.

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


Re: How to solve the given problem?

2022-02-10 Thread Chris Angelico
On Fri, 11 Feb 2022 at 02:15, NArshad  wrote:
>
> -ChrisA:
> You don't reply if you have problems.
> When I don't find any solution elsewhere then only I place in this group
>

You're a help vampire. Stop it.

https://slash7.com/2006/12/22/vampires/

Go do some actual research instead of asking people to do your homework.

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


Re: Multiple inheritance using super() in parent classes

2022-02-10 Thread Peter Otten

On 10/02/2022 09:20, Igor Basko wrote:

Hi everyone,
This is my first question here. Hope to get some clarification.
Basically this question is about multiple inheritance and the usage of
super().__init__ in parent
classes.

So I have two classes that inherit from the same base class.
For example class B and class C inherit from A:
class A:
   def __init__(self, arg1):
 pass

class B(A):
   def __init__(self, arg2):
 super().__init__(arg2)

class C(A):
   def __init__(self, arg1, arg2):
 super().__init__(arg2)

Now I would like to create a new class D that inherits from B and C.
One note, D is the only class that I am "allowed" to change. A, B and C are
provided to me as is from an external package.
class D(B, C):
   def __init__(self):
 B.__init__(self, 'arg1')
 C.__init__(self, 'arg1', 'arg2')

When I initialize D I get a TypeError.
TypeError: __init__() missing 1 required positional argument: 'arg2'
I get it from the invocation of super().__init__(arg2) inside the B class.

As I understand it, the super() inside B tries to call the __init__ of
class C,
because of the multiple inheritance and the MRO that is constructed.
But when B was implemented it wasn't aware of C and I assume,
B shouldn't be aware of C in any case.


Even when you call the initializers explicitly you pass self, and that's
probably where the MRO is taken from. You can tweak that MRO by changing
the order of the parent classes:

class D(C, B): ...

However, this will call B.__init__() twice; therefore I'd stick to super():

class D(C, B):
def __init__(self):
super().__init__("arg1", "arg2")


It gives me the feeling that I'm trying to implement some bad practice
here, but I'm not sure why.


I have the feeling that your feeling is right ;)


I would also like to hear your suggestions if there is a way to circumvent
it. Maybe by the approach described here:
https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
wrapping B and C in some Adapter class.


A quick glance at that page suggests that the adapter translates the
base class into an attribute -- which is what I would have suggested,
too. Using has-a instead of is-a relations is generally something to
consider seriously before jumping into multiple inheritance. The details
may depend on the actual use case which is hidden behind your A, B, C,
and D abstraction...

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


Re: How to solve the given problem?

2022-02-10 Thread Christian Gollwitzer

Am 10.02.22 um 11:26 schrieb NArshad:

-ChrisA:
You don't reply if you have problems.
When I don't find any solution elsewhere then only I place in this group


-Christian:
One problem of different type requires the same elaboration.


No it doesn't


Q. What technique of statistics or numerical computation or general mathematics 
to use to solve this problem. Can you tell one example

Find the values of 𝑥subscript(𝑖) for i from 1 to n (n =100).



WTF? Go study maths yourself instead of asking here (Offf-topic as well)

Christian

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


Re: Global VS Local Subroutines

2022-02-10 Thread BlindAnagram

On 10/02/2022 15:20, Chris Angelico wrote:

On Fri, 11 Feb 2022 at 02:13, BlindAnagram  wrote:


Is there any difference in performance between these two program layouts:

 def a():
   ...
 def(b):
   c = a(b)

or

 def(b):
   def a():
 ...
   c = a(b)

I would appreciate any insights on which layout to choose in which
circumstances.



Great question! The difference is that, in the second case, a() isn't
available anywhere else. So the real question is: Is that good or bad?

Does it make sense to call a() from outside of your second function,
even for testing? If so, maybe name it _a() so it's private, but keep
it global. Does a() need a lot of information from the context of your
second function? If so, it's easier to have a closure, with one
function inside another.

Both styles make sense, and your question is well put: it's a design
decision with no "right" or "wrong", just different choices with
different implications.

ChrisA


Thanks Chris, I thought it  was mostly a design choice but I wasn't sure 
whether there would be any significant performance issues.


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


Re: Global VS Local Subroutines

2022-02-10 Thread Rob Cliffe via Python-list



On 10/02/2022 12:13, BlindAnagram wrote:

Is there any difference in performance between these two program layouts:

   def a():
 ...
   def(b):
 c = a(b)

or

   def(b):
 def a():
   ...
 c = a(b)

I would appreciate any insights on which layout to choose in which 
circumstances.


The way to answer questions about performance is not to guess (often 
difficult or impossible), but to measure.  Profiling tools are available 
to see where a program spends how much of its time.  But a simpler 
approach might be to try to run the guts of your program 1000 or 100 
times, and find how long it takes with each layout (not with a stopwatch 
😁 but getting the program to record the start time, end time and the 
difference).

That said, I will venture a guess (or at least, an observation):
    def a():
        ...
is executable code.  It creates the function `a` which did not exist 
before (or creates the new version if it did).  This takes a certain 
amount of time.  In your 2nd layout, this overhead occurs every time b() 
is called.  So I would *guess* that this would be slower.  Of course it 
depends on how many times b() is called and probably on lots of other 
things.


But of course, performance is not the only consideration, as per Chris 
Angelico's answer.

Best wishes
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list


Re: Global VS Local Subroutines

2022-02-10 Thread Chris Angelico
On Fri, 11 Feb 2022 at 03:57, Rob Cliffe via Python-list
 wrote:
> But of course, performance is not the only consideration, as per Chris
> Angelico's answer.

Yep. In fact, I'd say that performance is the least significant
consideration here; do what makes sense. The time difference will be
negligible.

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


Python LSTM forecast future values for time series

2022-02-10 Thread Jorge Conforte


HI,


I'm starting run the LSTM to forecast future values for time serie data.

please can someone give me some information on how i can predict future 
values ​​for my time series using LSTM. Thanks, Conrado


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


GCP Copy Files - Data Export

2022-02-10 Thread BmoreIT
I did a data export from Google to export all company data - Google Data Export

It shows the root folder and to download, I run this command (it automatically 
enters this command)

gsutil -m cp -r \ "gs://takeout-export-myUniqueID" \.
But I have no idea where it would save it being I am not a GCP customer, only 
Google Workspace. Workspace won't help because they say it's a GCP product but 
I am exporting from Workspace. 

Can someone let me know the proper command to run on my local machine with 
Google's SDK to download this folder? I was able to start the download 
yesterday but it said there was an invalid character in the file names so it 
killed the export.

Appreciate any help!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Global VS Local Subroutines

2022-02-10 Thread BlindAnagram

On 10/02/2022 16:52, Rob Cliffe wrote:



On 10/02/2022 12:13, BlindAnagram wrote:

Is there any difference in performance between these two program layouts:

   def a():
 ...
   def(b):
 c = a(b)

or

   def(b):
 def a():
   ...
 c = a(b)

I would appreciate any insights on which layout to choose in which 
circumstances.


The way to answer questions about performance is not to guess (often 
difficult or impossible), but to measure.  Profiling tools are available 
to see where a program spends how much of its time.  But a simpler 
approach might be to try to run the guts of your program 1000 or 100 
times, and find how long it takes with each layout (not with a stopwatch 
😁 but getting the program to record the start time, end time and the 
difference).

That said, I will venture a guess (or at least, an observation):
     def a():
         ...


Thanks for the suggestion, Rob. I considered doing this but it would 
take quite a bit of careful work to be sure of obtaining meaningful 
results so I felt it better to ask the question here in case there was 
an quick answer on performance before doing the hard work.


is executable code.  It creates the function `a` which did not exist 
before (or creates the new version if it did).  This takes a certain 
amount of time.  In your 2nd layout, this overhead occurs every time b() 
is called.  So I would *guess* that this would be slower.  Of course it 
depends on how many times b() is called and probably on lots of other 
things.


This is exactly what I felt too but I then wondered if the code was 
recreated dynamically or was static with just a reference being created 
on each invocation of the parent.  The overhead in this case would be 
negligible. But then I thought 'what about the context for the inner 
function invocation' - maybe its not as simple as this!  Oh dear, I need 
to understand Python's internals.


At which point I asked the question!

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


Re: GCP Copy Files - Data Export

2022-02-10 Thread MRAB

On 2022-02-10 17:20, BmoreIT wrote:

I did a data export from Google to export all company data - Google Data Export

It shows the root folder and to download, I run this command (it automatically 
enters this command)

gsutil -m cp -r \ "gs://takeout-export-myUniqueID" \.
But I have no idea where it would save it being I am not a GCP customer, only 
Google Workspace. Workspace won't help because they say it's a GCP product but 
I am exporting from Workspace.

Can someone let me know the proper command to run on my local machine with 
Google's SDK to download this folder? I was able to start the download 
yesterday but it said there was an invalid character in the file names so it 
killed the export.

Appreciate any help!


On this page:

https://cloud.google.com/storage/docs/gsutil/commands/cp

I think the problem might be that you have an surplus backslash.

Does this work?

gsutil cp -r gs://takeout-export-myUniqueID .

This should copy recursively from gs://takeout-export-myUniqueID to the 
current folder.

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


Re: Global VS Local Subroutines

2022-02-10 Thread Eryk Sun
On 2/10/22, BlindAnagram  wrote:
>
> This is exactly what I felt too but I then wondered if the code was
> recreated dynamically or was static with just a reference being created
> on each invocation of the parent.  The overhead in this case would be
> negligible. But then I thought 'what about the context for the inner
> function invocation' - maybe its not as simple as this!  Oh dear, I need
> to understand Python's internals.

The bytecode for a() is a constant in b(). But the function object
itself isn't cached. Each time b() is called, a new function object
a() has to be created, with its defaults, keyword-only defaults, and
closure. The more complicated the latter are, the more work is
involved. Generally, however, the time to create a() will be an
insignificant fraction of the execution time of b(). If the latter
isn't the case, the cost only accumulates to something significant if
b() is called in a tight loop for an extended period.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2022-02-10 Thread Dennis Lee Bieber
On Wed, 9 Feb 2022 18:50:12 +, MRAB 
declaimed the following:

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

>> 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.
>
While not tested with Excel, I /have/ encountered cases where an
application has locked the file for writing, but multiple readers are
permitted. Those would fail then if one attempts to write. {The other view
point is a library that does a complete open/read\write-all/close to memory
-- such an application might open/read/close, then Excel opens/locks, with
the application only learning of the change when it attempts the
open/write/close cycle}


-- 
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2022-02-10 Thread Chris Angelico
On Fri, 11 Feb 2022 at 06:41, Dennis Lee Bieber  wrote:
>
> On Wed, 9 Feb 2022 18:50:12 +, MRAB 
> declaimed the following:
>
> >On 2022-02-09 12:45, Christian Gollwitzer wrote:
>
> >> 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.
> >
> While not tested with Excel, I /have/ encountered cases where an
> application has locked the file for writing, but multiple readers are
> permitted. Those would fail then if one attempts to write. {The other view
> point is a library that does a complete open/read\write-all/close to memory
> -- such an application might open/read/close, then Excel opens/locks, with
> the application only learning of the change when it attempts the
> open/write/close cycle}
>

Yeah, I doubt Excel is that sophisticated. It's built on an assumption
of single-user operation.

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


Re: How to solve the given problem?

2022-02-10 Thread Dennis Lee Bieber
On Wed, 9 Feb 2022 22:40:48 -0800 (PST), NArshad 
declaimed the following:

>
>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 very obviously phrased as a homework problem. WE DO NOT DO
HOMEWORK!

Write some Python code, present the code, your input data, and the
output it produced along with an example of the output you expected, and we
may be able to point out what parts of Python you made a mistake with. If
we get ambitious we might even correct an algorithm, not just explain
errors in the usage of Python.

I'm going a tad too far but...

HINT:
You need to compute the percentage of remaining scheduled units each
takes, then apply those percentages to the left-over from the mis-feed.

NONE of this has anything to do with Python itself -- it is purely
algorithm development that applies with any programming language -- or even
none.


-- 
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-10 Thread Jen Kris via Python-list
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); // Borrowed pointer

   // Then:  sentences = gutenberg.sents(fileid) - this is a sequence item
   PyObject *c_args = Py_BuildValue("s", pListStr);
   PyObject *args_tuple = PyTuple_New(1);
   PyTuple_SetItem(args_tuple, 0, c_args);

   pSents = PyObject_CallObject(pSentMod, args_tuple);

   if ( pSents == 0x0){
   PyErr_Print();
   return return_value; }

As you mentioned yesterday, CallObject needs a tuple, so that was the problem.  
Now it works.  

You also asked why I don't just use pListStrE.  I tried that and got a long 
error message from PyErr_Print.  I'm not far enough along in my C_API work to 
understand why, but it doesn't work.  

Thanks very much for your help on this.  

Jen


Feb 9, 2022, 17:40 by [email protected]:

> 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-10 Thread Jen Kris via Python-list
Hi and thanks very much for your comments on reference counting.  Since I'm new 
to the C_API that will help a lot.  I know that reference counting is one of 
the difficult issues with the C API.  

I just posted a reply to Inada Naoki showing how I solved the problem I posted 
yesterday.  

Thanks much for your help.

Jen


Feb 9, 2022, 18:43 by [email protected]:

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

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


Re: C API PyObject_Call segfaults with string

2022-02-10 Thread MRAB

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 (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); // Borrowed pointer

    // Then:  sentences = gutenberg.sents(fileid) - this is a sequence item
    PyObject *c_args = Py_BuildValue("s", pListStr);
    PyObject *args_tuple = PyTuple_New(1);
    PyTuple_SetItem(args_tuple, 0, c_args);

    pSents = PyObject_CallObject(pSentMod, args_tuple);

    if ( pSents == 0x0){
    PyErr_Print();
    return return_value; }

As you mentioned yesterday, CallObject needs a tuple, so that was the problem.  
Now it works.

You also asked why I don't just use pListStrE.  I tried that and got a long 
error message from PyErr_Print.  I'm not far enough along in my C_API work to 
understand why, but it doesn't work.

Thanks very much for your help on this.


You're encoding a Unicode string to a UTF-8 bytestring:

pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");

then pointing to the bytes of that UTF-8 bytestring:

pListStr = PyBytes_AS_STRING(pListStrE); // Borrowed pointer

then making a Unicode string from those UTF-8 bytes:

PyObject *c_args = Py_BuildValue("s", pListStr);

You might was well just use the original Unicode string!

Try this instead:

   Py_ssize_t listIndex = 0;
   pListItem = PyList_GetItem(pFileIds, listIndex);
   //> pListItem?

   pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem, 0);
   //> pSents+?

   if (pSents == 0x0){
   PyErr_Print();
   return return_value;
   }




Feb 9, 2022, 17:40 by [email protected]:


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.


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


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

2022-02-10 Thread Christian Gollwitzer

Am 10.02.22 um 20:43 schrieb Chris Angelico:

On Fri, 11 Feb 2022 at 06:41, Dennis Lee Bieber  wrote:

 While not tested with Excel, I /have/ encountered cases where an
application has locked the file for writing, but multiple readers are
permitted. Those would fail then if one attempts to write. {The other view
point is a library that does a complete open/read\write-all/close to memory
-- such an application might open/read/close, then Excel opens/locks, with
the application only learning of the change when it attempts the
open/write/close cycle}



Yeah, I doubt Excel is that sophisticated. It's built on an assumption
of single-user operation.



It guards against multiple user opening the same file over network 
drives. All MS applications create lock files with weird names like 
~.original-name.xlsx etc. If you open a file on the network share, and a 
colleague tries to open it from a second machine, then he will get the 
message "File locked by user xy from machine z". See here for word: 
https://support.microsoft.com/en-us/topic/-the-document-is-locked-for-editing-by-another-user-error-message-when-you-try-to-open-a-document-in-word-10b92aeb-2e23-25e0-9110-370af6edb638


I believe (haven't tested) that this is cooperative locking only and it 
doesn't help if you alter the file from another program. On the same 
machine though, I think that Excel opens the file with a flag to lock it 
from other processes. At least that was my observation and also what the 
OP has described.


Hence it is impossible to concurrently write from Python into an open 
Excel file. One might ask what the real problem is the user is trying to 
solve. Is Excel a requirement, can it be swapped by a database engine?


Best regards,

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


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

2022-02-10 Thread Chris Angelico
On Fri, 11 Feb 2022 at 07:57, Christian Gollwitzer  wrote:
>
> Am 10.02.22 um 20:43 schrieb Chris Angelico:
> > On Fri, 11 Feb 2022 at 06:41, Dennis Lee Bieber  
> > wrote:
> >>  While not tested with Excel, I /have/ encountered cases where an
> >> application has locked the file for writing, but multiple readers are
> >> permitted. Those would fail then if one attempts to write. {The other view
> >> point is a library that does a complete open/read\write-all/close to memory
> >> -- such an application might open/read/close, then Excel opens/locks, with
> >> the application only learning of the change when it attempts the
> >> open/write/close cycle}
> >>
> >
> > Yeah, I doubt Excel is that sophisticated. It's built on an assumption
> > of single-user operation.
> >
>
> It guards against multiple user opening the same file over network
> drives. All MS applications create lock files with weird names like
> ~.original-name.xlsx etc. If you open a file on the network share, and a
> colleague tries to open it from a second machine, then he will get the
> message "File locked by user xy from machine z". See here for word:
> https://support.microsoft.com/en-us/topic/-the-document-is-locked-for-editing-by-another-user-error-message-when-you-try-to-open-a-document-in-word-10b92aeb-2e23-25e0-9110-370af6edb638
>

Yeah, but that's still just hard locking, no "one writer multiple
readers" system or anything.

> I believe (haven't tested) that this is cooperative locking only and it
> doesn't help if you alter the file from another program. On the same
> machine though, I think that Excel opens the file with a flag to lock it
> from other processes. At least that was my observation and also what the
> OP has described.
>

That sounds right; and, again, it's just a simple exclusive lock.
Excel doesn't have the sophistication to need or want anything more
than simple "I have this file, nobody else touch it" exclusive
locking.

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


Re: Global VS Local Subroutines

2022-02-10 Thread Friedrich Rentsch
I believe to have observed a difference which also might be worth 
noting: the imbedded function a() (second example) has access to all of 
the imbedding function's variables, which might be an efficiency factor 
with lots of variables. The access is read-only, though. If the inner 
function writes to one of the readable external variables, that variable 
becomes local to the inner function.


Frederic

On 2/10/22 1:13 PM, BlindAnagram wrote:

Is there any difference in performance between these two program layouts:

   def a():
 ...
   def(b):
 c = a(b)

or

   def(b):
 def a():
   ...
 c = a(b)

I would appreciate any insights on which layout to choose in which 
circumstances.




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


Re: How do you log in your projects?

2022-02-10 Thread Marco Sulla
On Wed, 9 Feb 2022 at 20:40, Martin Di Paola  wrote:
>
> 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...").

? Logs are not intended to be read by end users. Logs are primarily
used to understand what the code is doing in a production environment.
They could also be used to gather metrics data.

Why should you log to give a message instead of simply using a print?

> For exceptions I print the message but not the traceback.

Why? Traceback is vital to understand what and where the problem is. I
think you're confusing logs with messages. The stack trace can be
logged (I would say must), but the end user generally sees a vague
message with some hints, unless the program is used internally only.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Global VS Local Subroutines

2022-02-10 Thread Marco Sulla
I agree with Chris. I don't know if it was already written: if you
want a local function for speed reasons, you can use the classic
approach of a main function.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to solve the given problem?

2022-02-10 Thread Marco Sulla
Narshad, I propose you post your questions to StackOverflow. I'm sure
they will be very happy.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_Call segfaults with string

2022-02-10 Thread Jen Kris via Python-list
Thank you for that suggestion.  It allowed me to replace six lines of code with 
one.  :)


Feb 10, 2022, 12:43 by [email protected]:

> 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 
>> (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); // Borrowed pointer
>>
>>     // Then:  sentences = gutenberg.sents(fileid) - this is a sequence item
>>     PyObject *c_args = Py_BuildValue("s", pListStr);
>>     PyObject *args_tuple = PyTuple_New(1);
>>     PyTuple_SetItem(args_tuple, 0, c_args);
>>
>>     pSents = PyObject_CallObject(pSentMod, args_tuple);
>>
>>     if ( pSents == 0x0){
>>     PyErr_Print();
>>     return return_value; }
>>
>> As you mentioned yesterday, CallObject needs a tuple, so that was the 
>> problem.  Now it works.
>>
>> You also asked why I don't just use pListStrE.  I tried that and got a long 
>> error message from PyErr_Print.  I'm not far enough along in my C_API work 
>> to understand why, but it doesn't work.
>>
>> Thanks very much for your help on this.
>>
> You're encoding a Unicode string to a UTF-8 bytestring:
>
>  pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
>
> then pointing to the bytes of that UTF-8 bytestring:
>
>  pListStr = PyBytes_AS_STRING(pListStrE); // Borrowed pointer
>
> then making a Unicode string from those UTF-8 bytes:
>
>  PyObject *c_args = Py_BuildValue("s", pListStr);
>
> You might was well just use the original Unicode string!
>
> Try this instead:
>
>  Py_ssize_t listIndex = 0;
>  pListItem = PyList_GetItem(pFileIds, listIndex);
>  //> pListItem?
>
>  pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem, 0);
>  //> pSents+?
>
>  if (pSents == 0x0){
>  PyErr_Print();
>  return return_value;
>  }
>
>>
>>
>> Feb 9, 2022, 17:40 by [email protected]:
>>
>>> 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.
>>>
> -- 
> https://mail.python.org/mailman/listinfo/python-list
>

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


Re: Global VS Local Subroutines

2022-02-10 Thread Rob Cliffe via Python-list



On 10/02/2022 21:43, Friedrich Rentsch wrote:
I believe to have observed a difference which also might be worth 
noting: the imbedded function a() (second example) has access to all 
of the imbedding function's variables, which might be an efficiency 
factor with lots of variables. The access is read-only, though. If the 
inner function writes to one of the readable external variables, that 
variable becomes local to the inner function.
You can make it continue to refer to the variables of the imbedding 
function, i.e. b(), by declaring them non-local, e.g.

    nonlocal c
Rob Cliffe



Frederic

On 2/10/22 1:13 PM, BlindAnagram wrote:
Is there any difference in performance between these two program 
layouts:


   def a():
 ...
   def(b):
 c = a(b)

or

   def(b):
 def a():
   ...
 c = a(b)

I would appreciate any insights on which layout to choose in which 
circumstances.






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


Re: How to solve the given problem?

2022-02-10 Thread Avi Gross via Python-list
Anyone think someone keeps asking homework questions? This seems absolutely 
unrelated to earlier discussions from this person. Jobs often tend to remain 
focused.
I opt out after frustration with earlier exchanges with NArshad about library 
books and EXCEL ...


-Original Message-
From: NArshad 
To: [email protected]
Sent: Thu, Feb 10, 2022 1:40 am
Subject: How to solve the given problem?


Assume that there is a pattern of feeding for a special fish in a day (10 hours 
a day) as below:
                              150    100    30    30    30    20    20    10    
5    5
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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Abstraction level at which to create SQLAlchemy ORM object

2022-02-10 Thread Cameron Simpson
On 10Feb2022 14:14, Loris Bennett  wrote:
>I am writing a command line program which will modify entries in a
>database and am trying out SQLAlchemy.
>
>A typical command might look like
>
>  um --operation add --uid ada --gid coders --lang en
>
>Parsing the arguments I get, ignoring the operation, a dict
>
>  {uid: "ada", gid: "coders", lang: "en"}
>
>At some point this needs to be converted into an object of the class User:
>
>  class User(Base):
>  __tablename__ = "users"
>
>  uid = Column('uid', String, primary_key=True)
>  gid = Column('gid', String)
>  lang = Column('lang', String)
>
>In a way it seems it would be economical to do the conversion as early
>as possible, so I can just pass around User objects.  However, this
>would mean that the entry point for the program would already be tightly
>coupled to the specifics of the database interaction.
>
>On the other hand, delaying the conversion would mean probably having to
>define my own User class, which seems like unnecessary overhead.  It
>would have the advantage that I could ditch SQLAlchemy more easily if I
>find it too mind-bending.

If the entire persistent state of the user lives in the db I'd just 
define the User ORM type and give it whatever methods you need. So 
exactly what you've got above.

It is close to the db, but if you only interact via the methods and the 
core attributes/columns that should be mostly irrelevant to you.

If you're concerned about switching backends, maybe define an 
AbstractUser abstract class with the required methods. Then you can at 
least ensure method coverage if you make another backend:

class AbstractUser(ABC):
@abstractmethod
def some_user_method(self,...):


class SQLAUser(Base, AbstractUser):
... your SQLA ORM User class above ...

User = SQLAUser

... everything else just talks about user ...

But you can do all of that _later_, only needed if you decide to change 
backends in a controlled manner.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2022-02-10 Thread Dennis Lee Bieber
On Thu, 10 Feb 2022 21:39:05 +0100, Christian Gollwitzer 
declaimed the following:


>Hence it is impossible to concurrently write from Python into an open 
>Excel file. One might ask what the real problem is the user is trying to 
>solve. Is Excel a requirement, can it be swapped by a database engine?
>

Based upon the path names shown by the OP, this is just a continuation
of the January thread...

Message-ID: <[email protected]>
Subject: What to write or search on github to get the code for what is
written below:
From: NArshad 
Injection-Date: Thu, 06 Jan 2022 18:55:30 +

... in which the OP insists they are required to manipulate a (never fully
specified) spreadsheet maintained in an Excel format file. The main
take-away in the current thread is that the OP has relinquished the idea of
a web-based application, for a local Tkinter GUI (and apparently has
actually written some code finally, as they produced a traceback message
sequence  -- however, the code-first_then-come-here lesson isn't
sticking if you look at their second thread of the week).


-- 
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you log in your projects?

2022-02-10 Thread Martin Di Paola

? Logs are not intended to be read by end users. Logs are primarily
used to understand what the code is doing in a production environment.
They could also be used to gather metrics data.

Why should you log to give a message instead of simply using a print?


You are assuming that logs and prints are different but they aren't. It 
is the same technology: some string formatted in a particular way sent 
to some place (console or file generally).


But using the logging machinery instead a plain print() you win a few 
features like thread safety and log levels (think in an user that wants 
to increase the verbose level of the output)


When communicating with an user, the logs that are intended to him/her 
can be sent to the console (like a print) in addition to a file.


For user's perspective, they look just like a print.


Why? Traceback is vital to understand what and where the problem is. I
think you're confusing logs with messages. The stack trace can be
logged (I would say must), but the end user generally sees a vague
message with some hints, unless the program is used internally only.


Yes, that's exactly why the traceback is hidden by default because the 
user don't care about it. If the error is due something that the user 
did wrong, then the message should say that and, if possible, add 
a suggestion of how to do it.


For example "The file 'foo.md' was not found." is quite descriptive. If 
you add to that message a traceback, that will just clutter the console.


Tracebacks and other errors and warnings must be logged in a file.  
I totally agree with that. Specially true when we are talking with 
server-like software.


Tracebacks can be printed to the user if a more verbose output is 
enabled. In that case you could even pretty print the traceback with 
syntax highlighting.


I guess that this should be discussed case by case. May be you are 
thinking more in a case where you have a service running and logging and 
I am more thinking in a program that a human executes by hand.


What info and how is presented to the user changes quite a bit.

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

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