Re: The slash "/" as used in the documentation
On 2/10/2019 11:32 PM, Ian Kelly wrote: On Sun, Feb 10, 2019 at 9:34 AM Chris Angelico wrote: Do you ACTUALLY want to call math.sin(x=1.234) or is it purely for the sake of consistency? Aside from questions about the help format, what is actually lost by the inability to pass those arguments by name? No, but I wouldn't object to d.get(key='foo', default=12). I don't see what dict acccess has to do with function calls. But moving on. It's partly about consistency, but mostly I was reacting to Terry's comments in that a) positional-only arguments are a "limitation" If one thinks of Python's flexible signatures as 'normal', which they aren't, than positional-only seems like a limitation. (Some other languages have somewhat similar flexibility.) From a C/assembler/machine perspective, positional is normal. and there is no desire for the rest of Python to match CPython's behavior in this instance, For reasons of complexity and speed, without a countervailing argument, there is no desire to add to C-coded functions a flexibility that will mostly not be used. and b) it sounds like a great deal of effort is being spent on updating all the C function signatures one at a time It tends to be done a module per PR. I don't know what effort is needed since I have never done it. so that the help string can be updated to a form that is unfamiliar and not intuitive '*' is not 'intuitive either. As I said elsewhere, multiple options were considered. '/' was the favorite because it is 'connected' to '*' and more intuitive for dividing a list into two sublists. Any, IDLE, but not help(), adds a comment to explain. "['/' marks preceding arguments as positional only]" The downside is that this and a following blank line add 2 lines to a calltip that is usually 2 lines, making it more obtrusive. And once '/' is understood, the comment is extra noise. So I am thinking of condensing it somehow. and not normally accessible to pure Python functions without some arm twisting. In my first response on this thread I explained and demonstrated how to access signature strings from Python, as done by both help() and IDLE. Please read if you are concerned about this. There is hardly any extra difficulty. I did not discuss how to process the signature string returned by str(signature) nor how to access the information in Signature objects otherwise, but the str and Signature docs cover these. It is possible the a sentence about '/' should be added somewhere. If every function has to be updated anyway, why not update them by fixing their signatures? I proposed this once and the answer was that it would be too costly. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: The slash "/" as used in the documentation
On Mon, Feb 11, 2019 at 12:19 AM Terry Reedy wrote: > The pass-by-position limitation is not in CPython, it is the behavior of > C functions, which is the behavior of function calls in probably every > assembly and machine language. Allowing the flexibility of Python > function calls take extra code and slows function calls. > > math.sin, for instance, is a fast-as-possible wrapper around the C math > library sin function. It extracts the C double from the Python float, > calls C sin, and returns the returned C double wrapped as a Python > float. Coredevs decided that being able to call math.sin(x=.3) is > not worth the resulting slowdown. I am rather sure that heavy users of > the math module would agree. For math.sin, sure, but what about, say, list.index? Here's the start of the implementation: static PyObject * listindex(PyListObject *self, PyObject *args) { Py_ssize_t i, start=0, stop=Py_SIZE(self); PyObject *v; if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &stop)) This is already paying the cost of not using C-style positional function arguments, but then it only takes an argument tuple, so it can't take keyword arguments anyway. Why? Wouldn't it be nice to be able to write: mylist.index(42, start=10, stop=99) instead of: mylist.index(42, 10, 99) This would also allow stop to be passed alone: mylist.index(42, stop=99) rather than having to pass the default value for start explicitly just so we can pass stop: mylist.index(42, 0, 99) -- https://mail.python.org/mailman/listinfo/python-list
Re: The use of type()
a = 5 print(a.__class__.__name__) int b = 5.0 print(b.__class__.__name__) float Thank you very much! :) ^Bart -- https://mail.python.org/mailman/listinfo/python-list
Re: The slash "/" as used in the documentation
On 2/10/2019 10:47 AM, Ian Kelly wrote: On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: This is the result of Python being a project of mostly unpaid volunteers. See my response in this thread explaining how '/' appears in help output and IDLE calltips. '/' only appears for CPython C-coded functions that have been modified to use Argument Clinic. A.C. was added, I believe, in 3.5. Simple builtins like len would have been the first to be converted. The math module was converted for 3.7. There are some new conversions for 3.8 and likely some will be left for future versions. I'm sure there are good reasons for it like most things Python does, but I can't help but wonder if working on removing the positional limitation from CPython would be a better use of time. The pass-by-position limitation is not in CPython, it is the behavior of C functions, which is the behavior of function calls in probably every assembly and machine language. Allowing the flexibility of Python function calls take extra code and slows function calls. math.sin, for instance, is a fast-as-possible wrapper around the C math library sin function. It extracts the C double from the Python float, calls C sin, and returns the returned C double wrapped as a Python float. Coredevs decided that being able to call math.sin(x=.3) is not worth the resulting slowdown. I am rather sure that heavy users of the math module would agree. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: The slash "/" as used in the documentation
On Sun, Feb 10, 2019 at 2:18 PM Avi Gross wrote: > I am not sure how python implements some of the functionality it does as > compared to other languages with similar features. But I note that there are > rules, presumably some for efficiency, such as requiring all keyword > arguments to be placed after the last positional argument. I mean I can call > func(a,b,c,d=1,e=2) but not func(a,b,d=1, c, e=2). > > So if you allowed a keyword to optionally be used for any positional > argument other than the last, c, would it not require a change in this rule? > I mean func(a="invalid",b,c,d=1,e=2) would violate the rule and so would > making b a keyword version. In my example, and for this reason only, maybe c > could work. My suggestion was not to allow keyword arguments to arbitrarily replace any positional parameter, or to otherwise change argument-passing in any way. The suggestion was to drop positional-only arguments from functions implemented in C instead of just documenting them better. In the example above, if you want to pass a by keyword, you would have to pass b and c by keyword as well. That would still be true for functions implemented in C if a, b, and c are no longer positional-only. > The original motivation for keyword arguments included the concept of > specifying a default if not used. Positional arguments have no default. > Similarly, they are required if explicitly named in the function definition. > So we are intermingling multiple concepts in the previous design. Positional arguments are allowed to have defaults, and keyword-only arguments are allowed to not have defaults. These are all valid syntax: # Allows a and b to be passed either positionally or by keyword def foo(a=1, b=2): pass # b is keyword-only def foo(a=1, *, b=2): pass # Allows a and b to be passed either positionally or by keyword def foo(a, b): pass # b is keyword-only and has no default def foo(a, *, b): pass Positional-ONLY arguments are not directly supported by the language, but they exist in the C API and can also have defaults. For example, dict.get takes two positional-only arguments. The second argument is optional, and its default is None. My point is that the axis of positional-only versus positional-or-keyword versus keyword-only, and the axis of required versus optional are entirely separate. > I won't go on except to say it would break lots of existing code and > potentially complicate new code. Can you give an example of something that would be broken by updating C API functions to name positional-only arguments instead of just updating them to document that they're positional-only? -- https://mail.python.org/mailman/listinfo/python-list
Re: The slash "/" as used in the documentation
On Sun, Feb 10, 2019 at 9:34 AM Chris Angelico wrote: > > On Mon, Feb 11, 2019 at 2:49 AM Ian Kelly wrote: > > > > On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: > > > > > > This is the result of Python being a project of mostly unpaid volunteers. > > > > > > See my response in this thread explaining how '/' appears in help output > > > and IDLE calltips. '/' only appears for CPython C-coded functions that > > > have been modified to use Argument Clinic. A.C. was added, I believe, > > > in 3.5. Simple builtins like len would have been the first to be > > > converted. The math module was converted for 3.7. There are some new > > > conversions for 3.8 and likely some will be left for future versions. > > > > I'm sure there are good reasons for it like most things Python does, but I > > can't help but wonder if working on removing the positional limitation from > > CPython would be a better use of time. > > Do you ACTUALLY want to call math.sin(x=1.234) or is it purely for the > sake of consistency? Aside from questions about the help format, what > is actually lost by the inability to pass those arguments by name? No, but I wouldn't object to d.get(key='foo', default=12). It's partly about consistency, but mostly I was reacting to Terry's comments in that a) positional-only arguments are a "limitation" and there is no desire for the rest of Python to match CPython's behavior in this instance, and b) it sounds like a great deal of effort is being spent on updating all the C function signatures one at a time so that the help string can be updated to a form that is unfamiliar and not intuitive and not normally accessible to pure Python functions without some arm twisting. If every function has to be updated anyway, why not update them by fixing their signatures? -- https://mail.python.org/mailman/listinfo/python-list
RE: The use of type()
Follow on to below. I was right and there is a fairly trivial and portable way to just show 'int' for the type of probably many types: No need to call the type() function at all. If "a" is an integer object containing "5" then you can ask for the class of it and then within that for the name like this: >>> a = 5 >>> print(a.__class__.__name__) int >>> b = 5.0 >>> print(b.__class__.__name__) float -Original Message- From: Python-list On Behalf Of Avi Gross Sent: Sunday, February 10, 2019 5:43 PM To: python-list@python.org Subject: RE: The use of type() Without using regular expressions, if you just want to extract the word "int" or "float" you can substring the results by converting what type says to a string: >>> a = 5 >>> str(type(a))[8:11] 'int' >>> a=5.0 >>> str(type(a))[8:13] 'float' Since the format and length vary, this may not meet your needs. You could search for the first index where there is a single quote and then the next and take what is in between. You can run this in-line or make a function that might work for at least the basic types: >>> a = 5 >>> text = str(type(a)) >>> first = text.find("'") >>> first += 1 >>> second = text.find("'", first) >>> first, second (8, 11) >>> text[first : second] 'int' >>> print(text[first : second]) Int If I do the same with a float like 5.0: >>> a=5.0 >>> text = str(type(a)) >>> first = text.find("'") >>> first += 1 >>> second = text.find("'", first) >>> print(text[first : second]) float For a list: >>> a = ["list", "of", "anything"] .. >>> print(text[first : second]) list Of course this is so simple it must be out there in some module. -Original Message- From: Python-list On Behalf Of ^Bart Sent: Sunday, February 10, 2019 4:43 PM To: python-list@python.org Subject: The use of type() I need to print something like "this variable is int" or "this variable is string" n1 = 10 n2 = 23 print ("Total of n1+n2 is: ",n1+n2," the type is", type(n1+n2)) When I run it I have: Total of n1+n2 is: 33 the type is >>> I'd like to read "the type is int" and NOT "the type is , how could I solve it? ^Bart -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
RE: The use of type()
Without using regular expressions, if you just want to extract the word "int" or "float" you can substring the results by converting what type says to a string: >>> a = 5 >>> str(type(a))[8:11] 'int' >>> a=5.0 >>> str(type(a))[8:13] 'float' Since the format and length vary, this may not meet your needs. You could search for the first index where there is a single quote and then the next and take what is in between. You can run this in-line or make a function that might work for at least the basic types: >>> a = 5 >>> text = str(type(a)) >>> first = text.find("'") >>> first += 1 >>> second = text.find("'", first) >>> first, second (8, 11) >>> text[first : second] 'int' >>> print(text[first : second]) Int If I do the same with a float like 5.0: >>> a=5.0 >>> text = str(type(a)) >>> first = text.find("'") >>> first += 1 >>> second = text.find("'", first) >>> print(text[first : second]) float For a list: >>> a = ["list", "of", "anything"] ... >>> print(text[first : second]) list Of course this is so simple it must be out there in some module. -Original Message- From: Python-list On Behalf Of ^Bart Sent: Sunday, February 10, 2019 4:43 PM To: python-list@python.org Subject: The use of type() I need to print something like "this variable is int" or "this variable is string" n1 = 10 n2 = 23 print ("Total of n1+n2 is: ",n1+n2," the type is", type(n1+n2)) When I run it I have: Total of n1+n2 is: 33 the type is >>> I'd like to read "the type is int" and NOT "the type is , how could I solve it? ^Bart -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: The use of type()
On Mon, Feb 11, 2019 at 8:46 AM ^Bart wrote: > > I need to print something like "this variable is int" or "this variable > is string" > > n1 = 10 > n2 = 23 > > print ("Total of n1+n2 is: ",n1+n2," the type is", type(n1+n2)) > > When I run it I have: > > Total of n1+n2 is: 33 the type is > >>> > > I'd like to read "the type is int" and NOT "the type is , > how could I solve it? > Many things in Python, including functions and types, have inherent names. >>> print(sys.__name__, int.__name__, print.__name__) sys int print Enjoy! ChrisA -- https://mail.python.org/mailman/listinfo/python-list
The use of type()
I need to print something like "this variable is int" or "this variable is string" n1 = 10 n2 = 23 print ("Total of n1+n2 is: ",n1+n2," the type is", type(n1+n2)) When I run it I have: Total of n1+n2 is: 33 the type is >>> I'd like to read "the type is int" and NOT "the type is , how could I solve it? ^Bart -- https://mail.python.org/mailman/listinfo/python-list
RE: Im trying to replicate the youtube video Creating my own customized celebrities with AI.
Bob, >> tenserflow, pygame, scipy, and numby > All of these are probably installable using pip. By the way did you mean numpy? > At a command prompt type pip install packagename. While you are correcting the spelling of downloads as the downloader is quite picky about exact spelling, please mention that the tenser flow should be tensorflow. I think numby and perhaps tense is how you feel as you read to the end of the documentation on numpy. More seriously, it helps if you have some idea of why these modules get names. NUMeric PYthon is numpy. Something like SCIentific PYthon is scipy. I am not as sure about TensorFlow but suspect the mathematical concept of a Tensor along with how it improves workflow. Haven't had a need to use it yet. Neither have I had a need for pygame which seems to be a way to make games in python. -Original Message- From: Python-list On Behalf Of Bob Gailer Sent: Sunday, February 10, 2019 12:07 PM To: jadenfig...@gmail.com Cc: python list Subject: Re: Im trying to replicate the youtube video Creating my own customized celebrities with AI. On Feb 10, 2019 11:40 AM, wrote: > > The video I don't see any video here. If you attached one the attachment did not come through. > gives the source code and the necessary things to download with it. > But I'm new to python and don't understand how to install any of the files. The files include: Python 3 Which you can download from python.org - just follow the link to downloads. > pip is automatically installed when you install python > tenserflow, pygame, scipy, and numby All of these are probably installable using pip. By the way did you mean numpy? At a command prompt type pip install packagename. I suggest you switch from l...@python.org to h...@python.org. Bob Gailer -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
RE: The slash "/" as used in the documentation
Chris, I would appreciate another pointer to the documentation explaining what was done and why as I deleted the earlier discussion. You ask: > Aside from questions about the help format, what is actually lost by the inability > to pass those arguments by name? I am not sure how python implements some of the functionality it does as compared to other languages with similar features. But I note that there are rules, presumably some for efficiency, such as requiring all keyword arguments to be placed after the last positional argument. I mean I can call func(a,b,c,d=1,e=2) but not func(a,b,d=1, c, e=2). So if you allowed a keyword to optionally be used for any positional argument other than the last, c, would it not require a change in this rule? I mean func(a="invalid",b,c,d=1,e=2) would violate the rule and so would making b a keyword version. In my example, and for this reason only, maybe c could work. And note the impact IF ALLOWED on the existing and new programs that allow a variable number of arguments of the form func(*args, **kwargs) when they evaluate. The previous model was that args would be a sequence of the arguments you could index so args[0] would be the first, or that you can pop off the front and use. If any of the previously un-named arguments can now entered with a keyword, are they now placed in args or in the dictionary kwargs? If in kwargs, the program now needs to know to look there in addition to the command line. If it was the first argument and is no longer in args, the second and further arguments would either be shifted over and be used wrong or you need a placeholder. The original motivation for keyword arguments included the concept of specifying a default if not used. Positional arguments have no default. Similarly, they are required if explicitly named in the function definition. So we are intermingling multiple concepts in the previous design. I won't go on except to say it would break lots of existing code and potentially complicate new code. Let me add a dumb suggestion. Anyone who desperately wants to name the parameters has a rather strange option they can do now. Very imperfect but consider the function prototype I have been using for illustration: func(a,b,c,d=1,e=2) It requires three positional arguments. What if you implement your code so some special values mean that you are going to pass along "a" as "key_a" instead. You can use something like None or the ellipsis(...) to signal this as in: def func(a, b, c, d=1, e=2, key_a=None): if a == ... : a = key_a print(a) The above does nothing but illustrates a WAY to allow a keyword implementation by using one or more placeholders as you can do the same gimmick for b and c. Here is how it runs if you use the positional arg, or an ellipsis and then a keyword or an ellipsis and accept the default for the keyword: >>> func(1,2,3) 1 >>> func(...,2,3,key_a="keyed up") keyed up >>> func(...,2,3) None So could you re-implement processing in a NEW language to allow different handling. I am guessing you could. Again, other languages currently do things their own way. But for compatibility, I can see why they may be patching and documenting what IS. And note you could create an amazingly annoying language where each parameter is specified as having umpteen attributes like it has to be the second un-named argument and of a restricted number of types and if it has a keyword it can be abbreviated as short as some string and then should it be placed in position 2 and does it have a default and who knows what more. Avi -Original Message- From: Python-list On Behalf Of Chris Angelico Sent: Sunday, February 10, 2019 11:32 AM To: Python Subject: Re: The slash "/" as used in the documentation On Mon, Feb 11, 2019 at 2:49 AM Ian Kelly wrote: > > On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: > > > > This is the result of Python being a project of mostly unpaid volunteers. > > > > See my response in this thread explaining how '/' appears in help > > output and IDLE calltips. '/' only appears for CPython C-coded > > functions that have been modified to use Argument Clinic. A.C. was > > added, I believe, in 3.5. Simple builtins like len would have been > > the first to be converted. The math module was converted for 3.7. > > There are some new conversions for 3.8 and likely some will be left for future versions. > > I'm sure there are good reasons for it like most things Python does, > but I can't help but wonder if working on removing the positional > limitation from CPython would be a better use of time. Do you ACTUALLY want to call math.sin(x=1.234) or is it purely for the sake of consistency? Aside from questions about the help format, what is actually lost by the inability to pass those arguments by name? ChrisA -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Im trying to replicate the youtube video Creating my own customized celebrities with AI.
On Feb 10, 2019 11:40 AM, wrote: > > The video I don't see any video here. If you attached one the attachment did not come through. > gives the source code and the necessary things to download with it. But I'm new to python and don't understand how to install any of the files. The files include: Python 3 Which you can download from python.org - just follow the link to downloads. > pip is automatically installed when you install python > tenserflow, pygame, scipy, and numby All of these are probably installable using pip. By the way did you mean numpy? At a command prompt type pip install packagename. I suggest you switch from l...@python.org to h...@python.org. Bob Gailer -- https://mail.python.org/mailman/listinfo/python-list
Re: where is math_sin defined?
On Mon, Feb 11, 2019 at 3:37 AM Barry Scott wrote: > > On Sunday, 10 February 2019 15:15:32 GMT Jon Ribbens wrote: > > As an aside, how is 'math.sin' actually implemented? mathmodule.c > > refers to the function 'math_sin' but that name is not defined > > anywhere in the Python source code. I'm a bit mystified as to how > > CPython manages to compile! > > I used gdb to find it: > Effective, if a little tedious. My technique was to first confirm that there was nothing saying "math_sin" anywhere in the repo (trust but verify - doesn't hurt to do a quick "git grep"), then to search mathmodule.c for "sin(", since searching for "sin" on its own gave way too many hits. That led me to the definition of sinpi(), then to asin() and sin(), both being defined using the FUNC1 template. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Im trying to replicate the youtube video Creating my own customized celebrities with AI.
The video gives the source code and the necessary things to download with it. But I'm new to python and don't understand how to install any of the files. The files include: Python 3, pip, tenserflow, pygame, scipy, and numby. Could anyone help me install all of this to run the AI. Thank you -- https://mail.python.org/mailman/listinfo/python-list
Re: where is math_sin defined?
On Sunday, 10 February 2019 15:15:32 GMT Jon Ribbens wrote: > As an aside, how is 'math.sin' actually implemented? mathmodule.c > refers to the function 'math_sin' but that name is not defined > anywhere in the Python source code. I'm a bit mystified as to how > CPython manages to compile! I used gdb to find it: Breakpoint 1, math_sin (self=, args=0) at / usr/src/debug/python3-3.7.2-4.fc29.x86_64/Modules/mathmodule.c:1176 1176FUNC1(sin, sin, 0, (gdb) l 1171 "remainder($module, x, y, /)\n--\n\n" 1172 "Difference between x and the closest integer multiple of y. \n\n" 1173 "Return x - n*y where n*y is the closest integer multiple of y. \n" 1174 "In the case where x is exactly halfway between two multiples of\n" 1175 "y, the nearest even value of n is used. The result is always exact.") 1176FUNC1(sin, sin, 0, 1177 "sin($module, x, /)\n--\n\n" 1178 "Return the sine of x (measured in radians).") 1179FUNC1(sinh, sinh, 1, 1180 "sinh($module, x, /)\n--\n\n" (gdb) Barry -- https://mail.python.org/mailman/listinfo/python-list
Re: The slash "/" as used in the documentation
On Mon, Feb 11, 2019 at 2:49 AM Ian Kelly wrote: > > On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: > > > > This is the result of Python being a project of mostly unpaid volunteers. > > > > See my response in this thread explaining how '/' appears in help output > > and IDLE calltips. '/' only appears for CPython C-coded functions that > > have been modified to use Argument Clinic. A.C. was added, I believe, > > in 3.5. Simple builtins like len would have been the first to be > > converted. The math module was converted for 3.7. There are some new > > conversions for 3.8 and likely some will be left for future versions. > > I'm sure there are good reasons for it like most things Python does, but I > can't help but wonder if working on removing the positional limitation from > CPython would be a better use of time. Do you ACTUALLY want to call math.sin(x=1.234) or is it purely for the sake of consistency? Aside from questions about the help format, what is actually lost by the inability to pass those arguments by name? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: C API PyObject_GetAttrString returns not the object I expected
On Sunday, 10 February 2019 13:58:57 GMT Stefan Behnel wrote: > Barry Scott schrieb am 10.02.19 um 13:08: > > After calling PyObject_GetAttrString() I expected to get a PyObject string > > back but I found that I had been given a instead. > > > > (gdb) p *args_o > > $4 = > > > > What is going on and how do I get from the to the > > object I want? > > Phil is right about the function itself, but my guess is that you called > GetAttr() on a class instead of an instance. Read up on Python descriptors > to understand what difference that makes. > > https://docs.python.org/3/howto/descriptor.html > > Basically, you got something like a property object back, but not the value > that the property maintaines. If you look up the attribute on the instance, > the property (or descriptor) will hand it to you. The same applies to > method lookups and other special attributes that may also be implemented as > descriptors. Thanks that the clue I needed. I had assumed that PyErr_Occurred() returned the instance, but it returns the type and as you explained that gives you the get_set_descriptor. I need to use PyErr_Fetch to get the instance. > Also take a look at Cython, which is designed to keep users from having to > learn all these things and instead lets you do them in Python. > > https://cython.org/ I'm adding better code to look at exceptions to PyCXX and hit this issue. Barry PyCXX maintainer. -- https://mail.python.org/mailman/listinfo/python-list
Re: [solved] C API version of str(exception) is not the same as pure python version
On Sunday, 10 February 2019 11:59:16 GMT Barry Scott wrote: > When I use the C API to get the str() of an execption I see this text: > > > > But python reports the following for the same exception: > > TypeError: unsupported operand type(s) for +: 'int' and 'str' > > What do I need to do in the C API to get the the same text for the > exception? > > PyObject *err = PyErr_Occurred(); > if( err != 0 ) > { > PyObject *u_why = PyObject_Str( err ); > PyObject *b_why = PyUnicode_AsEncodedString( u_why, "utf-8", > "strict" ); > int size = PyBytes_Size( b_why ); > char *why = PyBytes_AsString( b_why ); > printf("Error: %*s\n", size, why ); > return 0; > } Using this code fixes the problem as Stefan pointed out I had the type of the exception and not the value of the exception. if( PyErr_Occurred() ) { PyObject *type, *value, *trace; PyErr_Fetch( &type, &value, &trace ); PyObject *u_why = PyObject_Str( value ); PyObject *b_why = PyUnicode_AsEncodedString( u_why, "utf-8", "strict" ); int size = PyBytes_Size( b_why ); char *why = PyBytes_AsString( b_why ); printf("Error: %*s\n", size, why ); return 0; } -- https://mail.python.org/mailman/listinfo/python-list
Re: print console out to a txt or csv file
GISDude wrote: > Hi all, > I have been working on some code to list the files of a folder that has > .pdf extension. I have the basic code to create a list, but it prints that > list to the console. I'd like my code to write a txt file that contains > that list (to later import into excel). > > ###A script to list pdf files in a folder > ###gisdude 02/09/19 > ### > import os, sys, glob, csv, pathlib > > ###First, let's change the dir with os > > os.chdir("C:\\Users\\Randy\\Documents\\BOOKS") I have come to the conclusion that keeping track of the full path is much easier than keeping track of what directory you are in at the moment. I recommend that you never use os.chdir(). > ###Second, let's now get the list of files in this directory > > files = glob.glob('*.pdf') > for file in glob.glob("*.pdf"): > print (file) > > ###This prints to the IDLE console > ###But, I want to print to a csv file > > for filename in glob.iglob ('*.pdf'): > ###with open('Listofpdf', 'filename') as > ###csvfile: writer = csv.writer(csvfile, > ###delimter=' ', quotechar='|', > ###quoting=csv.QUOTE_MINIMAL) > ###writer.writerrow([('{}\t.format(elem))]) > from pathlib import Path > searchdir = "C:\\Users\\Randy\\Documents\\BOOKS" > csvfilename = "listofpdf.txt" > > with Path(csvfilename).open(mode="w+") as p: > files = Path(searchdir).glob('*.py') > p.write(f"{' '.join(str(file) for file in files)}\n") > > At this point, the pathlib mod seems like it should create the file? > > I'm on WINDOWS 10 and IDLE 3.7. > > Thanks for any help, > R` This looks rather messy, mainly because you are keeping every attempt you made. I recommend that you remove the code you don't use or that doesn't work as soon as possible. Here are two ways to write the list of filenames to a csv file: (1) Using traditional file path manipulation routines: PDFFOLDER = "C:\\Users\\Randy\\Documents\\BOOKS" CSVFILE = "files.txt" filenames = glob.glob(os.path.join(PDFFOLDER, "*.pdf")) with open(CSVFILE, "w", newline="") as outstream: writer = csv.writer(outstream) writer.writerows([path] for path in filenames) Note that you are actually writing a list of lists; omitting the inner list leads to writing one column per character in the filename. (2) Using pathlib's Path objects: PDFFOLDER = pathlib.Path("C:\\Users\\Randy\\Documents\\BOOKS") CSVFILENAME = pathlib.Path("files.txt") filenames = PDFFOLDER.glob("*.pdf") with CSVFILENAME.open("w", newline="") as outstream: writer = csv.writer(outstream) writer.writerows([path] for path in filenames) If you want the filename without directory replace path in the single item lists with os.path.basename(path) or path.name respectively. -- https://mail.python.org/mailman/listinfo/python-list
Re: The slash "/" as used in the documentation
On 2019-02-10, Chris Angelico wrote: > On Mon, Feb 11, 2019 at 2:21 AM Jon Ribbens wrote: >> On 2019-02-09, Terry Reedy wrote: >> > '/' is no uglier than, and directly analogous to, and as easy to produce >> > and comprehend, as '*'. It was selected after considerable discussion >> > of how to indicate that certain parameters are, at least in CPython, >> > positional only. The discussion of options included at least some of >> > those given above. It is very unlikely to go away or be changed. >> >> Ok, but what does it *mean*? > > It means "everything prior to this point is positional-only". > >> As an aside, how is 'math.sin' actually implemented? mathmodule.c >> refers to the function 'math_sin' but that name is not defined >> anywhere in the Python source code. I'm a bit mystified as to how >> CPython manages to compile! > > A lot of those sorts of functions are hyperthin wrappers around the C > math library. A bit of digging takes me to line 1176 of mathmodule.c > (in my source tree; exact line number may vary), which uses the > #define of FUNC1 from line 1032, and the actual code is up at line > 876, with a good block comment. Ah, it's using C preprocessor string concatenation to build the function name, which is why grepping for 'math_sin' didn't find it. Many thanks for your helpful answers. -- https://mail.python.org/mailman/listinfo/python-list
Fwd: improve TypeError for AsyncIterables when attempting to sync iterate
Currently when attempting to sync iterate an async iterable you get a TypeError: TypeError: 'SomeType' object is not iterable When attempting to iterate an async iterable (eg an object with a __aiter__ method the error could be something like: TypeError: 'SomeType' object is not iterable, it is however an AsyncIterable. Use `async for` instead. -- https://mail.python.org/mailman/listinfo/python-list
Re: The slash "/" as used in the documentation
On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: > > This is the result of Python being a project of mostly unpaid volunteers. > > See my response in this thread explaining how '/' appears in help output > and IDLE calltips. '/' only appears for CPython C-coded functions that > have been modified to use Argument Clinic. A.C. was added, I believe, > in 3.5. Simple builtins like len would have been the first to be > converted. The math module was converted for 3.7. There are some new > conversions for 3.8 and likely some will be left for future versions. I'm sure there are good reasons for it like most things Python does, but I can't help but wonder if working on removing the positional limitation from CPython would be a better use of time. -- https://mail.python.org/mailman/listinfo/python-list
Re: The slash "/" as used in the documentation
On Mon, Feb 11, 2019 at 2:21 AM Jon Ribbens wrote: > > On 2019-02-09, Terry Reedy wrote: > > '/' is no uglier than, and directly analogous to, and as easy to produce > > and comprehend, as '*'. It was selected after considerable discussion > > of how to indicate that certain parameters are, at least in CPython, > > positional only. The discussion of options included at least some of > > those given above. It is very unlikely to go away or be changed. > > Ok, but what does it *mean*? It means "everything prior to this point is positional-only". > As an aside, how is 'math.sin' actually implemented? mathmodule.c > refers to the function 'math_sin' but that name is not defined > anywhere in the Python source code. I'm a bit mystified as to how > CPython manages to compile! A lot of those sorts of functions are hyperthin wrappers around the C math library. A bit of digging takes me to line 1176 of mathmodule.c (in my source tree; exact line number may vary), which uses the #define of FUNC1 from line 1032, and the actual code is up at line 876, with a good block comment. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: The slash "/" as used in the documentation
On 2019-02-09, Terry Reedy wrote: > '/' is no uglier than, and directly analogous to, and as easy to produce > and comprehend, as '*'. It was selected after considerable discussion > of how to indicate that certain parameters are, at least in CPython, > positional only. The discussion of options included at least some of > those given above. It is very unlikely to go away or be changed. Ok, but what does it *mean*? As an aside, how is 'math.sin' actually implemented? mathmodule.c refers to the function 'math_sin' but that name is not defined anywhere in the Python source code. I'm a bit mystified as to how CPython manages to compile! -- https://mail.python.org/mailman/listinfo/python-list
print console out to a txt or csv file
Hi all, I have been working on some code to list the files of a folder that has .pdf extension. I have the basic code to create a list, but it prints that list to the console. I'd like my code to write a txt file that contains that list (to later import into excel). ###A script to list pdf files in a folder ###gisdude 02/09/19 ### import os, sys, glob, csv, pathlib ###First, let's change the dir with os os.chdir("C:\\Users\\Randy\\Documents\\BOOKS") ###Second, let's now get the list of files in this directory files = glob.glob('*.pdf') for file in glob.glob("*.pdf"): print (file) ###This prints to the IDLE console ###But, I want to print to a csv file for filename in glob.iglob ('*.pdf'): ###with open('Listofpdf', 'filename') as csvfile: ###writer = csv.writer(csvfile, delimter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) ###writer.writerrow([('{}\t.format(elem))]) from pathlib import Path searchdir = "C:\\Users\\Randy\\Documents\\BOOKS" csvfilename = "listofpdf.txt" with Path(csvfilename).open(mode="w+") as p: files = Path(searchdir).glob('*.py') p.write(f"{' '.join(str(file) for file in files)}\n") At this point, the pathlib mod seems like it should create the file? I'm on WINDOWS 10 and IDLE 3.7. Thanks for any help, R` -- https://mail.python.org/mailman/listinfo/python-list
Re: C API PyObject_GetAttrString returns not the object I expected
Barry Scott schrieb am 10.02.19 um 13:08: > After calling PyObject_GetAttrString() I expected to get a PyObject string > back but I found that I had been given a instead. > > (gdb) p *args_o > $4 = > > What is going on and how do I get from the to the object > I > want? Phil is right about the function itself, but my guess is that you called GetAttr() on a class instead of an instance. Read up on Python descriptors to understand what difference that makes. https://docs.python.org/3/howto/descriptor.html Basically, you got something like a property object back, but not the value that the property maintaines. If you look up the attribute on the instance, the property (or descriptor) will hand it to you. The same applies to method lookups and other special attributes that may also be implemented as descriptors. Also take a look at Cython, which is designed to keep users from having to learn all these things and instead lets you do them in Python. https://cython.org/ Stefan -- https://mail.python.org/mailman/listinfo/python-list
Re: C API PyObject_GetAttrString returns not the object I expected
On 10 Feb 2019, at 12:08 pm, Barry Scott wrote: > > After calling PyObject_GetAttrString() I expected to get a PyObject string > back but I found that I had been given a instead. > > (gdb) p *args_o > $4 = > > What is going on and how do I get from the to the object > I > want? The "String" part of "PyObject_GetAttrString()" refers to the way you provide the attribute name and not the type of the object returned. Phil -- https://mail.python.org/mailman/listinfo/python-list
C API PyObject_GetAttrString returns not the object I expected
After calling PyObject_GetAttrString() I expected to get a PyObject string back but I found that I had been given a instead. (gdb) p *args_o $4 = What is going on and how do I get from the to the object I want? Barry -- https://mail.python.org/mailman/listinfo/python-list
C API version of str(exception) is not the same as pure python version
When I use the C API to get the str() of an execption I see this text: But python reports the following for the same exception: TypeError: unsupported operand type(s) for +: 'int' and 'str' What do I need to do in the C API to get the the same text for the exception? All the code I'm using is below. I have been debugging this on Fedora 29 with python 3.7.2. To repoduce the problem put the three files in a folder and run: $ py3 setup.py build $ PYTHONPATH=build/lib.linux-x86_64-3.7 python3 simple_test.py Which will output: Error: Traceback (most recent call last): File "simple_test.py", line 7, in simple.test_error() File "simple_test.py", line 5, in simple_eval return eval( arg ) File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str' setup.py --- import os os.environ['CFLAGS'] = "-O0 -g" from distutils.core import setup, Extension setup(name = 'simple', version = '1.0', ext_modules = [Extension('simple', ['simple.c'])]) --- simple.c --- #include #include // Yes I know the ref counts are not decremented static PyObject *test_error(PyObject *self, PyObject *args) { PyObject *m = PyImport_AddModule( "__main__" ); PyObject *m_dict = PyModule_GetDict( m ); PyObject *func_args = PyTuple_New( 1 ); PyObject *str = PyUnicode_FromString( "1 + 'q'" ); PyTuple_SetItem( func_args, 0, str); PyObject *func = PyMapping_GetItemString( m_dict, "simple_eval" ); assert( func != 0 ); PyObject *result = PyObject_CallObject( func, func_args ); PyObject *err = PyErr_Occurred(); if( err != 0 ) { PyObject *u_why = PyObject_Str( err ); PyObject *b_why = PyUnicode_AsEncodedString( u_why, "utf-8", "strict" ); int size = PyBytes_Size( b_why ); char *why = PyBytes_AsString( b_why ); printf("Error: %*s\n", size, why ); return 0; } return result; } static PyMethodDef myMethods[] = { { "test_error", test_error, METH_NOARGS, "test eval with error" }, { NULL, NULL, 0, NULL } }; static struct PyModuleDef simple = { PyModuleDef_HEAD_INIT, "simple", "Simple Module", -1, myMethods }; // Initializes our module using our above struct PyMODINIT_FUNC PyInit_simple(void) { return PyModule_Create(&simple); } --- simple_test.py --- import sys import simple def simple_eval( arg ): return eval( arg ) simple.test_error() --- end -- Barry -- https://mail.python.org/mailman/listinfo/python-list