Re: we want python software
On Thu, Dec 7, 2017 at 6:54 PM, Marko Rauhamaa wrote: > Gregory Ewing : > >> Rick Johnson wrote: >>> DOLT: "Programming is easy! Once you learn the langauge, >>> it's just a matter of fill-in-the-blanks." >> >> To be fair to this person, for someone who has a natural aptitude for >> programming, it can be difficult to appreciate how hard it is for >> people who don't. >> >> When I first started programming, in my early teens, the basic ideas >> all seemed very straightforward, and I had no trouble seeing how to >> apply them. I unconsciously assumed it would be the same for anyone >> else with a reasonable level of intelligence. >> >> It was a while before it became clear to me that this is not the case >> at all. > > A junior programmer sees the unlimited possibilities of programming. No > montain is too high to climb. > > A seasoned programmer is elated if they can get anything to work at all. > And an expert programmer sees that those mountains are so full of bugs they're better described as ant-hills... ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: we want python software
> > A junior programmer sees the unlimited possibilities of programming. No > > montain is too high to climb. > > > > A seasoned programmer is elated if they can get anything to work at all. Good judgement comes from experience. And a lot of that comes from bad judgement. Karsten -- https://mail.python.org/mailman/listinfo/python-list
Re: Python homework
Il giorno mercoledì 6 dicembre 2017 02:33:52 UTC+1, nick martinez ha scritto: > I have a question on my homework. My homework is to write a program in which > the computer simulates the rolling of a die 50 > times and then prints > (i). the most frequent side of the die > (ii). the average die value of all rolls. > I wrote the program so it says the most frequent number out of all the rolls > for example (12,4,6,14,10,4) and will print out "14" instead of 4 like I need. > This is what I have so far: > import random > > def rollDie(number): > rolls = [0] * 6 > for i in range(0, number): > roll=int(random.randint(1,6)) > rolls[roll - 1] += 1 > return rolls > > if __name__ == "__main__": > result = rollDie(50) > print (result) > print(max(result)) Another way to get an answer is to use the numpy package. It is freely available and it is the "de facto" standard for numerical problems. If you get python from one of the scientific distribution (Anaconda, enthought, etc.) it will be automatically installed. import numpy Nrolls = 50 # but you can easily have millions of rolls a = numpy.random.randint(1,7,Nrolls) And then to count the rolls for each number you can use the histogram function frequency , _ = numpy.histogram(a, bins=[1,2,3,4,5,6,7]) You can now use the "argmax" method to find the number that appeared most frequently. Use the "mean" method to calculate the mean. Look on the help pages of numpy why I used radint(1,7) instead of radint(1,6) and the same for the bins in the histogram function. Have a look at: www.scipy.org Cheers :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Python homework
On Tuesday, December 5, 2017 at 8:33:52 PM UTC-5, nick martinez wrote:
> I have a question on my homework. My homework is to write a program in which
> the computer simulates the rolling of a die 50
> times and then prints
> (i). the most frequent side of the die
> (ii). the average die value of all rolls.
> I wrote the program so it says the most frequent number out of all the rolls
> for example (12,4,6,14,10,4) and will print out "14" instead of 4 like I need.
> This is what I have so far:
> import random
>
> def rollDie(number):
> rolls = [0] * 6
> for i in range(0, number):
> roll=int(random.randint(1,6))
> rolls[roll - 1] += 1
> return rolls
>
> if __name__ == "__main__":
> result = rollDie(50)
> print (result)
> print(max(result))
Just my 2 cents:
import random
def rollDie(number):
rolls = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0}
for i in range(0, number):
roll=int(random.randint(1,6))
rolls[roll] += 1
return rolls
if __name__ == "__main__":
rolls = rollDie(50)
most_rolled = rolls[max(rolls, key=lambda i: rolls[i])]
top_rollers = [k for k,v in rolls.items() if v == most_rolled]
print('The dice rolled: %s' % (rolls,))
if len(top_rollers) > 1:
message = 'The sides with most hits are %s, with %i hits each.'
else:
message = 'The side with most hits is %s, with %i hits'
print(message % (top_rollers, most_rolled))
# Sample output:
# The dice rolled: {1: 10, 2: 10, 3: 7, 4: 7, 5: 10, 6: 6}
# The sides with most hits are [1, 2, 5], with 10 hits each.
#
# The dice rolled: {1: 7, 2: 11, 3: 10, 4: 6, 5: 8, 6: 8}
# The side with most hits is [2], with 11 hits
--
https://mail.python.org/mailman/listinfo/python-list
Python script
Hi All, I am new to python need help to write a script in python my requirement is :- write a python script to print sentence from a txt file to another txt file Regards, Praveen -- https://mail.python.org/mailman/listinfo/python-list
Re: Python script
Am 07.12.17 um 15:06 schrieb [email protected]: Hi All, I am new to python need help to write a script in python my requirement is :- write a python script to print sentence from a txt file to another txt file txt = open("another.txt", "w") print("sentence from txt file", file = txt) Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: Python script
On Thu, Dec 7, 2017 at 9:06 AM, wrote: > Hi All, > I am new to python need help to write a script in python > my requirement is :- > write a python script to print sentence from a txt file to another txt file > > Regards, > Praveen > -- > https://mail.python.org/mailman/listinfo/python-list > So, the way it works here is that you should write some code as best you can. Paste it into your question using plaintext so that the formatting remains as coded, and tell us what errors you get (copy the traceback). People won't write code for you here, but they will help you figure out what you don't understand -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays -- https://mail.python.org/mailman/listinfo/python-list
Re: Python homework
On 07/12/17 13:19, Mario R. Osorio wrote: On Tuesday, December 5, 2017 at 8:33:52 PM UTC-5, nick martinez wrote: I have a question on my homework. [snip] Just my 2 cents: Sigh. Please don't do people's homework for them. It doesn't teach them anything. Now Nick had got 90% of the way there and shown his working, which is truly excellent, but what he needed was for someone to hint at how to search through the array, not to be handed a working example with no explanation. -- Rhodri James *-* Kynesim Ltd -- https://mail.python.org/mailman/listinfo/python-list
SystemError: error return without exception set
I am trying to use Python to communicate and send commands in MSC Marc. A part of the code looks something like this: from py_mentat import* directory = '[specified the file path to my model]' marcModel = '[name of my model]' py_echo(0) openModel = '*new_model yes *open_model "'+ directory + marcModel +'"' py_send(openModel) The code stops at "py_send(openModel)" with an error message that reads: Traceback (most recent call last): File "[my file path]", line 11, in py_send(openModel) SystemError: error return without exception set I tried running the code on different platforms (e.g. Command Prompt, Wing 101, python.exe) and I get the same result. What could be the problem? -- https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
On Thu, Dec 7, 2017 at 10:36 AM, Natalie Leung wrote: > I am trying to use Python to communicate and send commands in MSC Marc. A > part of the code looks something like this: > > from py_mentat import* > > directory = '[specified the file path to my model]' > marcModel = '[name of my model]' > > py_echo(0) > openModel = '*new_model yes *open_model "'+ directory + marcModel +'"' > py_send(openModel) > > > The code stops at "py_send(openModel)" with an error message that reads: > Traceback (most recent call last): File "[my file path]", line 11, in > py_send(openModel) > SystemError: error return without exception set > > I tried running the code on different platforms (e.g. Command Prompt, Wing > 101, python.exe) and I get the same result. What could be the problem? Faced with this I would use the debugger to break on the py_send line and then step into that function, and then step through it, e.g.: python -m pdb your_script.py b 8 [ or whatever the py_send line is) c [to continue to the BP) s [to step into py_send] then step through the function and something more informative may be revealed. -- https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
On 12/07/2017 04:36 PM, Natalie Leung wrote: > The code stops at "py_send(openModel)" with an error message that reads: > Traceback (most recent call last): File "[my file path]", line 11, in > py_send(openModel) > SystemError: error return without exception set > > I tried running the code on different platforms (e.g. Command Prompt, Wing > 101, python.exe) and I get the same result. What could be the problem? > I presume they have written an extension module, but haven't done their error handling correctly and are eating some internal information about the error that occurred. Judging by some quick searches online, the software is closed source right? In that case, you could try emailing the provider. If this is the case, you might try to attach to the extension code in a debugger, though I'd assume they've compiled it in a way to make that pretty difficult. Another approach might be not to use the python bindings, but to use whatever other binding are available (say in C?). Then you could try to set things up in the same way and see if an error occurs there and if that error has more information than yours. Cheers, Thomas -- https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
On Thursday, December 7, 2017 at 11:00:37 AM UTC-5, Thomas Nyberg wrote: > On 12/07/2017 04:36 PM, Natalie Leung wrote: > > The code stops at "py_send(openModel)" with an error message that reads: > > Traceback (most recent call last): File "[my file path]", line 11, in > > py_send(openModel) > > SystemError: error return without exception set > > > > I tried running the code on different platforms (e.g. Command Prompt, Wing > > 101, python.exe) and I get the same result. What could be the problem? > > > > I presume they have written an extension module, but haven't done their > error handling correctly and are eating some internal information about > the error that occurred. Judging by some quick searches online, the > software is closed source right? In that case, you could try emailing > the provider. > > If this is the case, you might try to attach to the extension code in a > debugger, though I'd assume they've compiled it in a way to make that > pretty difficult. Another approach might be not to use the python > bindings, but to use whatever other binding are available (say in C?). > Then you could try to set things up in the same way and see if an error > occurs there and if that error has more information than yours. > > Cheers, > Thomas Hi Thomas, You are correct in that the software and its packages are close-sourced. I have emailed the provider but their technical support staff has stated that they have exhausted of all ideas. An interesting thing to note is that this part of the code has been previously used by another individual with success, on the same computer. Kind regards, Natalie -- https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
On 2017-12-07 15:36, Natalie Leung wrote: I am trying to use Python to communicate and send commands in MSC Marc. A part of the code looks something like this: from py_mentat import* directory = '[specified the file path to my model]' marcModel = '[name of my model]' py_echo(0) openModel = '*new_model yes *open_model "'+ directory + marcModel +'"' py_send(openModel) The code stops at "py_send(openModel)" with an error message that reads: Traceback (most recent call last): File "[my file path]", line 11, in py_send(openModel) SystemError: error return without exception set I tried running the code on different platforms (e.g. Command Prompt, Wing 101, python.exe) and I get the same result. What could be the problem? It looks like it's calling some code written in C that's returning NULL, which indicates there's an error. The code should've set the exception that should be raised, but it hasn't done that. That's a bug in the C code. -- https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
On 12/07/2017 05:15 PM, Natalie Leung wrote: > Hi Thomas, > > You are correct in that the software and its packages are close-sourced. I > have emailed the provider but their technical support staff has stated that > they have exhausted of all ideas. > > An interesting thing to note is that this part of the code has been > previously used by another individual with success, on the same computer. > > Kind regards, > > Natalie > I would recommend a few things then. 0) See if you can access the code in the form that was previously sucessfully run and compare. To both versions do the following steps: 1) Make sure to rip out all extraneous code. I.e. only leave lines of code in if they absolutely need to be there. Hard-code paths, etc. Make sure you have an absolutely minimal script. 2) Make sure the file paths really are where the code thinks they are. Check permissions. In this case just write what the openModel string should be in directly (removing string concatenation). 3) Look at all the strings, paths, involved and see if things make sense. Also as an aside, look at the format of the string openModel and make sure it's a valid Marc command. It looks like it's basically a command in a domain specific language so that language should be documented and you might be able to find it. Good luck. Cheers, Thomas -- https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
On Fri, Dec 8, 2017 at 2:36 AM, Natalie Leung wrote: > I am trying to use Python to communicate and send commands in MSC Marc. A > part of the code looks something like this: > > from py_mentat import* > > directory = '[specified the file path to my model]' > marcModel = '[name of my model]' > > py_echo(0) > openModel = '*new_model yes *open_model "'+ directory + marcModel +'"' > py_send(openModel) > > > The code stops at "py_send(openModel)" with an error message that reads: > Traceback (most recent call last): File "[my file path]", line 11, in > py_send(openModel) > SystemError: error return without exception set > > I tried running the code on different platforms (e.g. Command Prompt, Wing > 101, python.exe) and I get the same result. What could be the problem? In its purest sense, that error is a bug inside a Python extension module (as others have mentioned). Since the failure is basically "hey, you said you raised an exception, but I can't find the exception you thought you were raising", it's quite probable that there is a bug in your Python code; go back to the tutorial for Marc, and see if you can find an issue. Unfortunately you're flying blind here, but maybe you can figure something out even without the usual help of the error message. But regardless, the bug first and foremost is in the extension module. The module's creator should be able to audit the py_send function to figure out where it is returning NULL without calling one of the "raise exception" functions. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Stackoverflow question: Is there a built-in identity function in Python?
The simple answer is No, and all the answers agree on that point.
It does beg the question of what an identity function is, though.
My contention is that an identity function is a do-nothing function that simply
returns what it was given:
--> identity(1)
1
--> identity('spam')
'spam'
--> identity('spam', 'eggs', 7)
('spam', 'eggs', 7)
Of the five answers to that SO question, mine is the only one that will correctly handle those three examples. If you
agree with my contention feel free to up-vote my answer. :)
https://stackoverflow.com/a/8748063/208880
--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
On 2017-12-07 17:22, Chris Angelico wrote: On Fri, Dec 8, 2017 at 2:36 AM, Natalie Leung wrote: I am trying to use Python to communicate and send commands in MSC Marc. A part of the code looks something like this: from py_mentat import* directory = '[specified the file path to my model]' marcModel = '[name of my model]' py_echo(0) openModel = '*new_model yes *open_model "'+ directory + marcModel +'"' py_send(openModel) The code stops at "py_send(openModel)" with an error message that reads: Traceback (most recent call last): File "[my file path]", line 11, in py_send(openModel) SystemError: error return without exception set I tried running the code on different platforms (e.g. Command Prompt, Wing 101, python.exe) and I get the same result. What could be the problem? In its purest sense, that error is a bug inside a Python extension module (as others have mentioned). Since the failure is basically "hey, you said you raised an exception, but I can't find the exception you thought you were raising", it's quite probable that there is a bug in your Python code; go back to the tutorial for Marc, and see if you can find an issue. Unfortunately you're flying blind here, but maybe you can figure something out even without the usual help of the error message. But regardless, the bug first and foremost is in the extension module. The module's creator should be able to audit the py_send function to figure out where it is returning NULL without calling one of the "raise exception" functions. It's probably that the code is assuming that call somewhere to the Python API is returning a reference, but sometimes it isn't. _All_ such return values should be checked for NULL. -- https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
On Fri, Dec 8, 2017 at 5:28 AM, MRAB wrote: > On 2017-12-07 17:22, Chris Angelico wrote: >> >> On Fri, Dec 8, 2017 at 2:36 AM, Natalie Leung >> wrote: >>> >>> I am trying to use Python to communicate and send commands in MSC Marc. A >>> part of the code looks something like this: >>> >>> from py_mentat import* >>> >>> directory = '[specified the file path to my model]' >>> marcModel = '[name of my model]' >>> >>> py_echo(0) >>> openModel = '*new_model yes *open_model "'+ directory + marcModel +'"' >>> py_send(openModel) >>> >>> >>> The code stops at "py_send(openModel)" with an error message that reads: >>> Traceback (most recent call last): File "[my file path]", line 11, in >>> py_send(openModel) >>> SystemError: error return without exception set >>> >>> I tried running the code on different platforms (e.g. Command Prompt, >>> Wing 101, python.exe) and I get the same result. What could be the problem? >> >> >> In its purest sense, that error is a bug inside a Python extension >> module (as others have mentioned). Since the failure is basically >> "hey, you said you raised an exception, but I can't find the exception >> you thought you were raising", it's quite probable that there is a bug >> in your Python code; go back to the tutorial for Marc, and see if you >> can find an issue. Unfortunately you're flying blind here, but maybe >> you can figure something out even without the usual help of the error >> message. >> >> But regardless, the bug first and foremost is in the extension module. >> The module's creator should be able to audit the py_send function to >> figure out where it is returning NULL without calling one of the >> "raise exception" functions. >> > It's probably that the code is assuming that call somewhere to the Python > API is returning a reference, but sometimes it isn't. > > _All_ such return values should be checked for NULL. Mmm true. Forgot about that part. So, yes, that's a different type of failure that could be happening. Makes the audit a bit harder. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Python script
On Thursday, December 7, 2017 at 2:06:46 PM UTC, [email protected] wrote: > Hi All, > I am new to python need help to write a script in python > my requirement is :- > write a python script to print sentence from a txt file to another txt file > > Regards, > Praveen Read this https://docs.python.org/3/tutorial/inputoutput.html. Run your favourite editor, type your code and save it. Try running the script and when you hit problems show us your code, the input, the expected output and state exactly what happened. Then we'll happily help you. -- Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python installer hangs in Windows 7
On Monday, December 4, 2017 at 3:44:48 PM UTC-5, [email protected] wrote: > Same with me, except that I tried to install Python 3.6.3. Unchecking > "Install launcher for all users" helped, however. I'm having the same issue. I need Python to be available for all users. Has anyone found a solution? I'm using Windows 7, by the way. -- https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
Ethan Furman wrote:
> The simple answer is No, and all the answers agree on that point.
>
> It does beg the question of what an identity function is, though.
>
> My contention is that an identity function is a do-nothing function that
> simply returns what it was given:
>
> --> identity(1)
> 1
>
> --> identity('spam')
> 'spam'
>
> --> identity('spam', 'eggs', 7)
> ('spam', 'eggs', 7)
Hm, what does -- and what should --
identity(('spam', 'eggs', 7))
produce?
>
> Of the five answers to that SO question, mine is the only one that will
> correctly handle those three examples. If you
> agree with my contention feel free to up-vote my answer. :)
>
> https://stackoverflow.com/a/8748063/208880
>
> --
> ~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On Fri, Dec 8, 2017 at 5:53 AM, Peter Otten <[email protected]> wrote: > Ethan Furman wrote: > >> The simple answer is No, and all the answers agree on that point. >> >> It does beg the question of what an identity function is, though. >> >> My contention is that an identity function is a do-nothing function that >> simply returns what it was given: >> >> --> identity(1) >> 1 >> >> --> identity('spam') >> 'spam' >> >> --> identity('spam', 'eggs', 7) >> ('spam', 'eggs', 7) > > Hm, what does -- and what should -- > > identity(('spam', 'eggs', 7)) > > produce? The same thing. And so should identity((('spam', 'eggs', 7))) and identity'spam', 'eggs', 7 and identity('spam', 'eggs', 7). For consistency, identity 'spam', 'eggs', 7 should work too. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On 12/07/2017 10:53 AM, Peter Otten wrote:
Ethan Furman wrote:
The simple answer is No, and all the answers agree on that point.
It does beg the question of what an identity function is, though.
My contention is that an identity function is a do-nothing function that
simply returns what it was given:
--> identity(1)
1
--> identity('spam')
'spam'
--> identity('spam', 'eggs', 7)
('spam', 'eggs', 7)
Hm, what does -- and what should --
identity(('spam', 'eggs', 7))
produce?
Well, since it's the lowly "," that makes a tuple (not the parentheses), those
extra parentheses don't have any affect.
If you were trying to get a 3-item tuple inside a 1-item tuple:
(('spam', 'eggs', 7), )
Then you would need:
--> identity( (('spam', 'eggs', 7), ) )
(('spam', 'eggs', 7),)
Okay, actually sometimes it takes both. ;)
--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
Chris Angelico writes: > On Fri, Dec 8, 2017 at 5:28 AM, MRAB wrote: >> It's probably that the code is assuming that call somewhere to the Python >> API is returning a reference, but sometimes it isn't. >> >> _All_ such return values should be checked for NULL. > > Mmm true. Forgot about that part. So, yes, that's a different type > of failure that could be happening. Makes the audit a bit harder. More probably the problem is not directly from a Python API, as most of the time when such APIs return NULL they also call PyErr_SetXXX() appropriately. ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. [email protected] | -- Fortunato Depero, 1929. -- https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On 12/7/17 1:28 PM, Ethan Furman wrote:
The simple answer is No, and all the answers agree on that point.
It does beg the question of what an identity function is, though.
My contention is that an identity function is a do-nothing function
that simply returns what it was given:
--> identity(1)
1
--> identity('spam')
'spam'
--> identity('spam', 'eggs', 7)
('spam', 'eggs', 7)
I don't see why this last case should hold. Why does the function take
more than one argument? And if it does, then why doesn't it work like this?
--> identity('spam')
('spam',)
(because then it wouldn't be an identity function!) Trying to handle
the multi-argument case seems like it adds an unneeded special case to
the function.
--Ned.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
Chris Angelico writes: > On Fri, Dec 8, 2017 at 5:53 AM, Peter Otten <[email protected]> wrote: >> >> Hm, what does -- and what should -- >> >> identity(('spam', 'eggs', 7)) >> >> produce? > > The same thing. And so should identity((('spam', 'eggs', 7))) and > identity'spam', 'eggs', 7 and identity('spam', 'eggs', > 7). > > For consistency, identity 'spam', 'eggs', 7 should work too. So you think that identity('spam', 'eggs', 7) \ == identity(('spam', 'eggs', 7)) \ == identity((('spam', 'eggs', 7),)) \ == identity'spam', 'eggs', 7),),)) should yield True? ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. [email protected] | -- Fortunato Depero, 1929. -- https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On Fri, Dec 8, 2017 at 6:29 AM, Lele Gaifax wrote: > Chris Angelico writes: > >> On Fri, Dec 8, 2017 at 5:53 AM, Peter Otten <[email protected]> wrote: >>> >>> Hm, what does -- and what should -- >>> >>> identity(('spam', 'eggs', 7)) >>> >>> produce? >> >> The same thing. And so should identity((('spam', 'eggs', 7))) and >> identity'spam', 'eggs', 7 and identity('spam', 'eggs', >> 7). >> >> For consistency, identity 'spam', 'eggs', 7 should work too. > > So you think that > > identity('spam', 'eggs', 7) \ > == identity(('spam', 'eggs', 7)) \ > == identity((('spam', 'eggs', 7),)) \ > == identity'spam', 'eggs', 7),),)) > > should yield True? No, because you're adding commas. Commas are, like, really important, yo? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On 12/07/2017 11:23 AM, Ned Batchelder wrote:
On 12/7/17 1:28 PM, Ethan Furman wrote:
--> identity('spam', 'eggs', 7)
('spam', 'eggs', 7)
I don't see why this last case should hold. Why does the function take more
than one argument? And if it does, then
why doesn't it work like this?
--> identity('spam')
('spam',)
(because then it wouldn't be an identity function!) Trying to handle the
multi-argument case seems like it adds an
unneeded special case to the function.
--> a = 'spam'
--> a == neds_identity(a)
False
;)
--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On 7 December 2017 at 18:28, Ethan Furman wrote:
> The simple answer is No, and all the answers agree on that point.
>
> It does beg the question of what an identity function is, though.
>
> My contention is that an identity function is a do-nothing function that
> simply returns what it was given:
>
> --> identity(1)
> 1
>
> --> identity('spam')
> 'spam'
>
> --> identity('spam', 'eggs', 7)
> ('spam', 'eggs', 7)
>
> Of the five answers to that SO question, mine is the only one that will
> correctly handle those three examples. If you agree with my contention feel
> free to up-vote my answer. :)
IMO (as a mathematician ;-)) the identity function is a
*single-argument* function that returns the value passed to it. So:
def identity(x):
return x
See https://en.wikipedia.org/wiki/Identity_function
identity(1,2) is an error.
Extending the definition to multiple arguments causes all sorts of
confusion, as you've seen.
Paul
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On 12/07/2017 11:46 AM, Paul Moore wrote:
On 7 December 2017 at 18:28, Ethan Furman wrote:
The simple answer is No, and all the answers agree on that point.
It does beg the question of what an identity function is, though.
My contention is that an identity function is a do-nothing function that
simply returns what it was given:
--> identity(1)
1
--> identity('spam')
'spam'
--> identity('spam', 'eggs', 7)
('spam', 'eggs', 7)
Of the five answers to that SO question, mine is the only one that will
correctly handle those three examples. If you agree with my contention feel
free to up-vote my answer. :)
IMO (as a mathematician ;-)) the identity function is a
*single-argument* function that returns the value passed to it. So:
def identity(x):
return x
See https://en.wikipedia.org/wiki/Identity_function
identity(1,2) is an error.
Extending the definition to multiple arguments causes all sorts of
confusion, as you've seen.
So in other words:
for thing in (
1,
(2, 3),
'spam',
('eggs', 'green', 4.15),
(1, ),
):
assert thing == identity(thing)
try:
identity('too', 'many', 'things')
except TypeError:
pass
else:
raise Exception('identity should only be passed a single item')
Thank you for clearing that up, Paul!
I up-voted your answer, hopefully others will also:
https://stackoverflow.com/a/47702881/208880
--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
There is a built-in identity function in Python. The function is called
'id'. See https://docs.python.org/3/library/functions.html#id Note that
this will not behave the same across different Python runtimes. e.g.
CPython, IronPython or Jython all implement this differently.
An example:
Python 3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> b = 2
>>> id(a)
10911168
>>> id(b)
10911200
>>> c = 1
>>> id (c)
10911168
Regards,
Nathan
On Thu, Dec 7, 2017 at 1:46 PM, Paul Moore wrote:
> On 7 December 2017 at 18:28, Ethan Furman wrote:
> > The simple answer is No, and all the answers agree on that point.
> >
> > It does beg the question of what an identity function is, though.
> >
> > My contention is that an identity function is a do-nothing function that
> > simply returns what it was given:
> >
> > --> identity(1)
> > 1
> >
> > --> identity('spam')
> > 'spam'
> >
> > --> identity('spam', 'eggs', 7)
> > ('spam', 'eggs', 7)
> >
> > Of the five answers to that SO question, mine is the only one that will
> > correctly handle those three examples. If you agree with my contention
> feel
> > free to up-vote my answer. :)
>
> IMO (as a mathematician ;-)) the identity function is a
> *single-argument* function that returns the value passed to it. So:
>
> def identity(x):
> return x
>
> See https://en.wikipedia.org/wiki/Identity_function
>
> identity(1,2) is an error.
>
> Extending the definition to multiple arguments causes all sorts of
> confusion, as you've seen.
>
> Paul
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
Ethan Furman wrote:
> On 12/07/2017 10:53 AM, Peter Otten wrote:
>> Ethan Furman wrote:
>>
>>> The simple answer is No, and all the answers agree on that point.
>>>
>>> It does beg the question of what an identity function is, though.
>>>
>>> My contention is that an identity function is a do-nothing function that
>>> simply returns what it was given:
>>>
>>> --> identity(1)
>>> 1
>>>
>>> --> identity('spam')
>>> 'spam'
>>>
>>> --> identity('spam', 'eggs', 7)
>>> ('spam', 'eggs', 7)
>>
>> Hm, what does -- and what should --
>>
>> identity(('spam', 'eggs', 7))
>>
>> produce?
>
> Well, since it's the lowly "," that makes a tuple (not the parentheses),
> those extra parentheses don't have any affect.
identity((a, b, c))
calls identity() with one argument whereas
identity(a, b, c)
calls identity() with three arguments. That's certainly an effect; you just
undo it with your test for len(args) == 1. That means that your identity()
function throws away the information about the number of arguments it was
called with. I would expect an identity() function to be lossless
("bijective") and I think that is possible only if you restrict it to a
single argument.
>
> If you were trying to get a 3-item tuple inside a 1-item tuple:
>
> (('spam', 'eggs', 7), )
>
> Then you would need:
>
> --> identity( (('spam', 'eggs', 7), ) )
> (('spam', 'eggs', 7),)
>
> Okay, actually sometimes it takes both. ;)
>
> --
> ~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On 12/7/17 2:41 PM, Ethan Furman wrote:
On 12/07/2017 11:23 AM, Ned Batchelder wrote:
On 12/7/17 1:28 PM, Ethan Furman wrote:
--> identity('spam', 'eggs', 7)
('spam', 'eggs', 7)
I don't see why this last case should hold. Why does the function
take more than one argument? And if it does, then
why doesn't it work like this?
--> identity('spam')
('spam',)
(because then it wouldn't be an identity function!) Trying to handle
the multi-argument case seems like it adds an
unneeded special case to the function.
--> a = 'spam'
--> a == neds_identity(a)
False
Right, but why does one argument return the argument, but n>1 returns a
tuple of the args?
--Ned.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On Fri, Dec 8, 2017 at 7:25 AM, Ned Batchelder wrote:
> On 12/7/17 2:41 PM, Ethan Furman wrote:
>>
>> On 12/07/2017 11:23 AM, Ned Batchelder wrote:
>>>
>>> On 12/7/17 1:28 PM, Ethan Furman wrote:
>>
>>
--> identity('spam', 'eggs', 7)
('spam', 'eggs', 7)
>>>
>>>
>>> I don't see why this last case should hold. Why does the function take
>>> more than one argument? And if it does, then
>>> why doesn't it work like this?
>>>
>>> --> identity('spam')
>>> ('spam',)
>>>
>>> (because then it wouldn't be an identity function!) Trying to handle the
>>> multi-argument case seems like it adds an
>>> unneeded special case to the function.
>>
>>
>> --> a = 'spam'
>> --> a == neds_identity(a)
>> False
>>
>
> Right, but why does one argument return the argument, but n>1 returns a
> tuple of the args?
>
Because it's impossible to return multiple values. IMO the "identity
function" is defined only in terms of one single argument, so all of
this is meaningless.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On 12/07/2017 12:24 PM, Peter Otten wrote:
identity((a, b, c))
calls identity() with one argument whereas
identity(a, b, c)
calls identity() with three arguments. That's certainly an effect; you just
undo it with your test for len(args) == 1. That means that your identity()
function throws away the information about the number of arguments it was
called with. I would expect an identity() function to be lossless
("bijective") and I think that is possible only if you restrict it to a
single argument.
Thanks for exposing/clarifying that flaw. I have removed my answer.
--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On Fri, Dec 8, 2017 at 8:30 AM, Lele Gaifax wrote: > Chris Angelico writes: > >> On Fri, Dec 8, 2017 at 6:29 AM, Lele Gaifax wrote: >>> Chris Angelico writes: >>> On Fri, Dec 8, 2017 at 5:53 AM, Peter Otten <[email protected]> wrote: > > Hm, what does -- and what should -- > > identity(('spam', 'eggs', 7)) > > produce? The same thing. And so should identity((('spam', 'eggs', 7))) and identity'spam', 'eggs', 7 and identity('spam', 'eggs', 7). For consistency, identity 'spam', 'eggs', 7 should work too. >>> >>> So you think that >>> >>> identity('spam', 'eggs', 7) \ >>> == identity(('spam', 'eggs', 7)) \ >>> == identity((('spam', 'eggs', 7),)) \ >>> == identity'spam', 'eggs', 7),),)) >>> >>> should yield True? >> >> No, because you're adding commas. Commas are, like, really important, yo? > > I probably misunderstood your "The same thing" answer to Peter's question, > but IMHO `identity(('spam','eggs',7))` should not return the same as > `identity('spam','eggs',7)` as I got from your answer, should it? > That's exactly the point under discussion. According to Ethan's posted implementation, multiple arguments get collected into a tuple. That means that passing three arguments results in a tuple, but passing a single argument (that happens to be a tuple of three things) returns that argument unchanged. So, yes, the two would return the same value. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
Chris Angelico writes: > On Fri, Dec 8, 2017 at 6:29 AM, Lele Gaifax wrote: >> Chris Angelico writes: >> >>> On Fri, Dec 8, 2017 at 5:53 AM, Peter Otten <[email protected]> wrote: Hm, what does -- and what should -- identity(('spam', 'eggs', 7)) produce? >>> >>> The same thing. And so should identity((('spam', 'eggs', 7))) and >>> identity'spam', 'eggs', 7 and identity('spam', 'eggs', >>> 7). >>> >>> For consistency, identity 'spam', 'eggs', 7 should work too. >> >> So you think that >> >> identity('spam', 'eggs', 7) \ >> == identity(('spam', 'eggs', 7)) \ >> == identity((('spam', 'eggs', 7),)) \ >> == identity'spam', 'eggs', 7),),)) >> >> should yield True? > > No, because you're adding commas. Commas are, like, really important, yo? I probably misunderstood your "The same thing" answer to Peter's question, but IMHO `identity(('spam','eggs',7))` should not return the same as `identity('spam','eggs',7)` as I got from your answer, should it? ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. [email protected] | -- Fortunato Depero, 1929. -- https://mail.python.org/mailman/listinfo/python-list
Re: SystemError: error return without exception set
On Friday, December 8, 2017 at 5:15:35 AM UTC+13, Natalie Leung wrote: > I have emailed the provider but their technical support staff has stated > that they have exhausted of all ideas. If they can’t supply you with a product fit for purpose, time to send it back for a refund. -- https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On 7 December 2017 at 20:35, Chris Angelico wrote: > Because it's impossible to return multiple values. IMO the "identity > function" is defined only in terms of one single argument, so all of > this is meaningless. Indeed, this is the key point. The Python language only allows returning one value (which can be a tuple, sure, but it's still *one value*). So a function that returns what it's called with can only have one argument. Anything else isn't an "identity function". Certainly, it might be useful - "def f(arg, *rest, **kw): return arg" could be a useful dummy function in some contexts, for example - but it's not an identity function in the strict sense (and so you can't avoid having to specify its behaviour explicitly). Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
Ethan Furman writes:
> My contention is that an identity function is a do-nothing function
> that simply returns what it was given:
>
> --> identity(1)
> 1
>
> --> identity('spam')
> 'spam'
These seem good to me. One argument given, the same result returned.
> --> identity('spam', 'eggs', 7)
> ('spam', 'eggs', 7)
That's a confusingly inconsistent result, as pointed out by other
responses in this thread.
I would expect an identity function to accept exactly one positional
argument. Given more than one positional argument, it should raise a
TypeError.
--
\ Legionnaire: “We have their leader captive!” Cæsar: “Is he |
`\ bound?” Legionnaire: “Of his health I know not, sir.” —The |
_o__)Goon Show, _The Histories Of Pliny The Elder_ |
Ben Finney
--
https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
Nathan Ernst writes: > There is a built-in identity function in Python. The function is called > 'id'. It should be clear from the rest of the thread. But, to be explicit: That's not what is meant by “identity function”, and the Python ‘id’ function is not an identity function. The Python ‘id’ function returns the “identity of the object”, a Python-specific concept that is unrelated to the identity function. An “identity function” is mathematics terminology for “a function that always returns the same value that was used as its argument.” https://en.wikipedia.org/wiki/Identity_function> -- \ “But Marge, what if we chose the wrong religion? Each week we | `\ just make God madder and madder.” —Homer, _The Simpsons_ | _o__) | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Please tell me how to execute python file in Ubuntu by double clicking on file. (Posting On Python-List Prohibited)
On Wednesday, December 6, 2017 at 8:29:23 PM UTC-6, Steve D'Aprano wrote: [...] > If the term "OS file associations" is ever so slightly > inaccurate (it's not the actual OS kernel that does the > associating, but the desktop environment), well, we can > probably say the same thing about Mac OS X and maybe even > Windows itself. I don't believe many folks think about the OS in the strictly narrow view that it is "only a kernel existing below higher abstractions", as much as they accept the OS as encompassing the OS kernel and all the ooey and gooey interfacing layers on top. I suppose you might call that a "system", but such a word is far too ambiguous. IOWs: it may not be technically correct, but it is the accepted norm. -- https://mail.python.org/mailman/listinfo/python-list
Re: Please tell me how to execute python file in Ubuntu by double clicking on file. (Posting On Python-List Prohibited)
On Thu, Dec 07, 2017 at 01:29:11PM +1100, Steve D'Aprano wrote: > On Thu, 7 Dec 2017 08:22 am, Python wrote: > >> > Linux doesn’t do “OS file associations”. > >> > >> Then how does my Linux box know that when I double-click on a text file, it > >> launches kwrite rather than (say) the Gimp or LibreOffice? > > > > The answer to that is (sadly) complicated. > > Actually, no, the answer to my question is very simple: Lawrence is mistaken > about Linux not doing file associations. It does -- it is merely handled by > the desktop environment (if there is one). Pedantically speaking, this is only *probably true*, not certainly true (e.g. running Linux on a text console with something like midnight commander, some unrelated file manager while running a particular desktop environment, etc.). But more importantly, practically speaking, it still doesn't really provide much more help to the OP than Lawrence's answer. He may well know already that the desktop environment is what does the job (and probably does even, in broad terms, if he's familiar with computers in general), but have no idea how to configure it. A reasonably helpful answer would be one that mentioned a few of the likely possibilities (Gnome, KDE, Unity, /etc/mime.types, "other"), and gave hints for how to find out the answer for each. A thoroughly helpful answer would be, well, outside the scope of this list/group. Pedantry has its place, FWIW. In the computer field, as with other science and engineering disciplines, often precision is much more essential than in other fields. I personally find such precision is especially warranted if you take it upon yourself to criticize what someone else has said. Though, providing such precision via natural language often turns out to be more challenging than one would hope... -- https://mail.python.org/mailman/listinfo/python-list
Re: Stackoverflow question: Is there a built-in identity function in Python?
On 12/07/2017 10:28 AM, Ethan Furman wrote: The simple answer is No, and all the answers agree on that point. It does beg the question of what an identity function is, though. Thankfully, Paul answered that question with a good explanation*. Thanks, everyone, for the discussion. -- ~Ethan~ * https://stackoverflow.com/a/47702881/208880 -- https://mail.python.org/mailman/listinfo/python-list
Re: why won't slicing lists raise IndexError?
Hi Rick! On Wed, Dec 06, 2017 at 04:05:42PM -0800, Rick Johnson wrote: > Python wrote: > > [...] > > > THIS IS FALSE. CALLING A FUNCTION > > What *FUNCTION*? In this snippet (which again, we agreed was an incomplete academic example): if item: process(item) else: do_without_item() Can you please explain to me what sort of Python syntactical construct do_without_item() could be, other than a call to a Python callable object (i.e. a function)? Then, could you explain to me how that particular syntactical construct is in any way equivalent to the pass statement? Could you then explain to me how using that in the simple example given makes any sense whatsoever? [FWIW, if there is an answer to this, I am genuinely interested...] Since it can indeed be a function call, then if you can't do all of the above things, doesn't that imply that in the example given, do_witout_item() can't be anything BUT a function call? Even if it somehow doesn't by some logic that escapes me, for the purposes of this simple academic example, isn't it preferable to assume that it is the most obvious thing (i.e. a function call)? Isn't the else clause in fact NOT superfluous, because it illustrates that if the slice operation would be out of the container object's bounds, then item will be assigned a value that evaluates to false, enabling one to trigger the else clause of an if block to do something ONLY under that condition? > if "do_without_item()" had been defined, then you could call > it a function. But until you do, it's just a NameError. OK! Then could you explain to me how a NameError is equivalent to the pass statement? I thought one continued execution without any side effects, and the other required handling or else will stop the program. Isn't that in fact functionally nonequivalent? Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Anything similar to __END__ in perl
Hi, perl has __END__ which ignore all the lines below it. Is there anything similar to __END__ in python? Thanks. -- Regards, Peng -- https://mail.python.org/mailman/listinfo/python-list
