Re: Python running issues

2019-04-19 Thread dieter
Kiranpreet Kaur  writes:
> I am trying to run python from command prompt. However, cannot get past the
> error: “ImportError: no module named site”

Your Python installation is severely broken.

I cannot tell you why. I would try a reinstallation (and see
whether the problem disappears).

If the problem does not disappear, come back with the information
on which operating system you are using Python.
I might be able to help for "*nix" like systems
others for "Windows".

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


Re: Questions on Instance methods

2019-04-19 Thread dieter
Arup Rakshit  writes:

>>When an instance method object is created by retrieving a class method 
>> object from a class or instance, its __self__ attribute is the class itself, 
>> and its __func__ attribute is the function object underlying the class 
>> method.
>
> Here I have 2 questions:
>
> 1. How do you create an instance method object from a class method object by 
> using either the class or the instance?

Typically, it happens automatically by accessing "instance.method".
The "types" module contains a type which allows you to
create instance methods manually. However, in this case,
you decide what becomes its "__self__" and its "__function__".

> 2. Why in both cases the __self__ is set to Class only?

Because of the "class method".

The "*method" objects have the task to provide the (implicit) first
argument to the function. For a function defined as "classmethod",
this is the class (otherwise the instance).

> 3. Would you give me examples also while explaining this?

python3
>>> class C:
...   @classmethod
...   def cm(cls): print(cls)
...   def im(self): print(self)
... 
>>> C.cm
>
>>> c=C()
>>> c.cm
>
>>> c.im
>
>>> C.cm.__self__

>>> c.cm.__self__

>>> c.im.__self__
<__main__.C object at 0xb785258c>

>>   When an instance method object is derived from a class method object, the 
>> “class instance” stored in __self__ will actually be the class itself, so 
>> that calling either x.f(1) or C.f(1) is equivalent to calling f(C,1) where f 
>> is the underlying function.

I agree that the above paragraph could be improved.

It addresses the case "cm" above: you access a method
defined as a class method (the "is derived from a class method object" above
means "is created for the access to a class method").
The paragraph wants to stress that even though the (bound method) attribute
is named "__self__", it actually contains the class and not a
(class) instance.


> Here x is an instance of C. Would you give me an example to illustrate why " 
> x.f(1) or C.f(1) is equivalent to calling f(C,1)” ?

Remember that the purpose of the "*method" objects it to automatically
provide the first argument ("cls" or "self") to the function.

Because accessing a method automatically creates a method
object, it is non trivial to access the "underlying function".
One possibility is to use "__func__" on the method object.
With this in mind we get for the above example:

>>> c.cm.__func__(C)

>>> c.cm()

>>> C.cm.__func__(C)

>>> C.cm()


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


Re: Function to determine list max without itertools

2019-04-19 Thread Michael Torrie
On 04/19/2019 04:01 AM, Sayth Renshaw wrote:
> def max_try(listarg):
> myMax = listarg[0]
> try:
> for item in listarg:
> if item > myMax:
> myMax = item
> except TypeError:
> print(f'Only numbers are supported, this entry "{item}" was not')
> pass
> 
> return myMax

If I were you I wouldn't catch that exception.  The reason is that a
non-number is something your code just can't handle correctly anyway, so
better to let that original exception bubble up to the caller who can
deal with it.  By catching this exception, your code will fail, but
still return as if it succeeded.

Others may disagree. But I rarely catch exceptions in my code unless my
code specifically wants or needs to deal with them.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to determine list max without itertools

2019-04-19 Thread Rob Gaddi

On 4/19/19 5:34 PM, MRAB wrote:

[snip]


How would you avoid comparing the initial max with list[0]? By slicing 
the list? By using 'iter' and 'next'? Do you expect that doing either of 
those to avoid a single comparison would make it faster? Is a comparison 
very expensive?


You could, of course, do some benchmarks to see if it makes a 
difference, but, personally, I'd just leave it.


Personally yes, but because it allows it to execute on non-sequence 
iterables rather than because it saves a single near-instantaneous 
comparison.



--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Function to determine list max without itertools

2019-04-19 Thread MRAB

On 2019-04-20 00:24, DL Neil wrote:

On 20/04/19 4:41 AM, Rob Gaddi wrote:

On 4/19/19 12:23 AM, Sayth Renshaw wrote:

On Friday, 19 April 2019 17:01:33 UTC+10, Sayth Renshaw  wrote:

Set the first item in the list as the current largest.
 Compare each subsequent integer to the first.
 if this element is larger, set integer.


def maxitwo(listarg):
 myMax = listarg[0]
 for item in listarg:
 if item > myMax:
 myMax = item

 return myMax


When you understand what it is you intend to write (barring DL Neil's 
comments), and THEN write it, you write the correct thing.  Thus endith 
the lesson.



+1, Rob's guidance saves time and embarrassment...


[snip]

Regarding 'optimisation': rather than 'disappearing' into high-volume
and 'exotic' situations (see earlier comment), why not stick with the
simple stuff? For example, once 'max' is initialised, is there a need to
compare max with 'list[ 0 ]'?


Is that really a problem?

How would you avoid comparing the initial max with list[0]? By slicing 
the list? By using 'iter' and 'next'? Do you expect that doing either of 
those to avoid a single comparison would make it faster? Is a comparison 
very expensive?


You could, of course, do some benchmarks to see if it makes a 
difference, but, personally, I'd just leave it.

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


Re: Function to determine list max without itertools

2019-04-19 Thread Cameron Simpson

On 19Apr2019 05:38, Dan Sommers <2qdxy4rzwzuui...@potatochowder.com> wrote:

On 4/19/19 4:01 AM, Sayth Renshaw wrote:


Now, what happens when the code is tested with various (different) 
sets of test-data?

(remember the last question from my previous msg!?)


It fails on any list entry that isn't a float or an int, giving a TypeError.


What if the *first* entry isn't a float or an int?  For
instance, what if the list is ['a', 'b', 'c']?  What about
['a', 'b', 3]?


Maybe it doesn't matter. Consider: the notion of a maximum implies that 
all the items are comparable, otherwise you can't tell if one item is 
the max versus another.


Provided all elements are mutually comparable eg all numeric or all 
strings, his code is fine.


As soon as you want to mix values, you are making a _policy_ decision.  


Are mixed values ok at all? If not, his code is fine.

Should values all be mutually comparable, _except_ for some special 
values? Such as None or the values NaN float values?


Should failing comparisons be ignored? (In which case the first element 
might dictate the definition of a valid comparison.)


Should they raise an exception (which his code will, for free)?

All these questions are policy because the assume some greater context 
not provided in the problem definition.


Sayth has only one decision to make here:

Is the basic algorithm all that is required: assume all values are 
mutually comparable? In many situations that will do very well.


Or should the basic algorithm "handle" noncomparable values?

Unfortunately the meaning of "handle" might go several ways: at least 
the basic form alerts you to the fact that there are noncomparable 
values.


Personally, I'd go one of 2 paths:

- leave the code alone - it a simple and understandable and assumes 
 _nothing_ about the values other than comparableness


- provide a test for _expected_ incomparable values as an additional 
 argument and use it to filter out these: any other values which fail 
 to compare represent bad input, and the function _should_ fail


But for the latter Python already has a builtin filter() function which 
the caller could trivially use to pre-filter the input data, avoiding 
any need to pointlessly complicate the basic maximum function.


So I'd say Sayth has attained the target goal.

Cheers,
Cameron Simpson 

Q: How does a hacker fix a function which doesn't work for all of the 
elements in its domain?

A: He changes the domain.
- Rich Wareham 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Function to determine list max without itertools

2019-04-19 Thread DL Neil

On 20/04/19 4:41 AM, Rob Gaddi wrote:

On 4/19/19 12:23 AM, Sayth Renshaw wrote:

On Friday, 19 April 2019 17:01:33 UTC+10, Sayth Renshaw  wrote:

Set the first item in the list as the current largest.
 Compare each subsequent integer to the first.
 if this element is larger, set integer.


def maxitwo(listarg):
 myMax = listarg[0]
 for item in listarg:
 if item > myMax:
 myMax = item

 return myMax


When you understand what it is you intend to write (barring DL Neil's 
comments), and THEN write it, you write the correct thing.  Thus endith 
the lesson.



+1, Rob's guidance saves time and embarrassment...

Upon opening a new module in one's text-editor/IDE one of the first 
steps should be to write an explanatory docstring. Similarly, after 
typing the letters "def" or "class"!


On the basis of this conversation, you might also benefit from reading 
about the use of doctests! (seeing we're talking docstrings - can always 
move to other test methods later)


I find this a useful habit, particularly if I am outlining an entire 
module or class, and only later coming back to flesh-out the code.


Further reading: Test-Driven Development


Of course all the comments in the world won't help if code != comment!!! 
Before congratulating ourselves that the code 'works': did you spot the 
difference in the better-developed 'English' description and the 
implementation in Python?



Regarding 'testing': Start with the scope and your objectives [as yet 
unstated, so we can only imagine what they might be]. When should it 
work, eg data-types. Why doesn't the code work with string data? Does it 
work if the list mixes different data-types? Should it work if a list 
element is itself a collection/class?


Regarding 'optimisation': rather than 'disappearing' into high-volume 
and 'exotic' situations (see earlier comment), why not stick with the 
simple stuff? For example, once 'max' is initialised, is there a need to 
compare max with 'list[ 0 ]'?



Extension: if, instead of merely finding the largest value in the list 
(cf which element of the list is the largest!), what if the code also 
'moved' that element to the end of the list? Then, what if that function 
was called again but this time to operate on all of the list EXCEPT the 
last (which we already know is the largest) - and if this 'outer loop' 
were repeated 'len( list )' number of times (either by loop or (perhaps 
a later coding exercise) by recursion; what would be the final condition 
of the 'list'?


--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python running issues

2019-04-19 Thread Bob Gailer
please copy everything from the command you entered through the end of the
error message. Then paste that into a reply email. Also let us know what
your operating system is. Be sure to reply all so a copy goes to the list.

Bob Gailer

On Apr 19, 2019 6:56 PM, "Kiranpreet Kaur"  wrote:

Hello,



I am trying to run python from command prompt. However, cannot get past the
error: “ImportError: no module named site”, whenever I try to run Python
from the terminal. Can you help me fix that? I spent a couple hours on
searching a fix for this issue on Google and nothing seems to work. I even
deleted and re-installed Python, updated the Environment variables in the
System Settings, but nothing seems to work.



Also, I am not able to  install the library needed to run the pip command.



I would really appreciate if you could help me.







Best Regards,

Kiranpreet Kaur

kayk...@ucdavis.edu
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Python running issues

2019-04-19 Thread Kiranpreet Kaur
Hello,



I am trying to run python from command prompt. However, cannot get past the
error: “ImportError: no module named site”, whenever I try to run Python
from the terminal. Can you help me fix that? I spent a couple hours on
searching a fix for this issue on Google and nothing seems to work. I even
deleted and re-installed Python, updated the Environment variables in the
System Settings, but nothing seems to work.



Also, I am not able to  install the library needed to run the pip command.



I would really appreciate if you could help me.







Best Regards,

Kiranpreet Kaur

kayk...@ucdavis.edu
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Enhanced input dialog

2019-04-19 Thread Chris Angelico
On Sat, Apr 20, 2019 at 7:16 AM  wrote:
>
> Running Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 
> 32 bit (Intel)] on win32.
> Under Win7 using Pyscripter 3.6.0.0 x86
> Somehow  integer1 = input( "Enter first integer:\n" ) # read string
> when executed  presents a nice input dialog box  instead of a
> command line prompt.
> It  provides a text box,  OK and Exit buttons,
> and I did not import wxPython.
> The print command responds as expected.
> How is this possible ?
> What is providing this enhanced functionality ?

Since input() is just a function, you should be able to explore it.
Start by looking at input.__module__ and see what it was imported from
- that'll most likely give you a good clue.

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


Enhanced input dialog

2019-04-19 Thread srfpala
Running Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 
bit (Intel)] on win32.
Under Win7 using Pyscripter 3.6.0.0 x86
Somehow  integer1 = input( "Enter first integer:\n" ) # read string  
when executed  presents a nice input dialog box  instead of a
command line prompt.   
It  provides a text box,  OK and Exit buttons, 
and I did not import wxPython.
The print command responds as expected.
How is this possible ?
What is providing this enhanced functionality ?
TIA
  Bob
-- 
https://mail.python.org/mailman/listinfo/python-list


Reg : Data Analysis using python

2019-04-19 Thread Sushanth
Hi team,

Election Analytics
 mock view

-- 
*Regards*
*Sushanth*
*ph : 91-9444583307*
-- 
https://mail.python.org/mailman/listinfo/python-list


Re:

2019-04-19 Thread Shakti Kumar
On Fri, 19 Apr 2019 at 11:19 PM Tamara Berger  wrote:

> Hi Shaki,
>
> Thanks for your reply. I tried your code, but it didn't work. Here is the
> section of code and your addition:
>
>if ((monthly_salary*t)*compound_int)   print('It is not possible to save for the downpayment in 36 months.')
>   break
>   import sys
>   sys.exit()
>

Include the sys.exit() before the break.
You cannot have two branching statements in the same block.
So it'd be,

if ((monthly_salary*t)*compound_int) Thanks,
> Tamara
>
>
> On Fri, Apr 19, 2019 at 1:23 PM Shakti Kumar <
> shakti.shrivastav...@gmail.com> wrote:
>
>>
>>
>> On Fri, 19 Apr 2019 at 9:33 PM Tamara Berger  wrote:
>>
>>> Hi Python-List,
>>>
>>> What code can I use to break out of a program completely, and not just
>>> out
>>> of a loop?
>>
>>
>> import sys
>> sys.exit()
>>
>> Should do your work.
>>
>>>
>>
>> I wrote code with 3 conditions for saving for a downpayment. The
>>> first addresses cases that don't meet the minimum condition; i.e., enough
>>> money to save for a downpayment within the allotted time. It has its own
>>> print line, but also executes the irrelevant print lines for the other
>>> two
>>> conditions.
>>
>>
>> However anyone would suggest to put the prints in the proper if else
>> block rather than going for an exit.
>>
>>>
>>
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>
>>
>> Thanks,
>> Shakti.
>>
>>> 
>>>
>> --
>> Sent from Shakti’s iPhone
>>
> --
Sent from Shakti’s iPhone
-- 
https://mail.python.org/mailman/listinfo/python-list


Re:

2019-04-19 Thread Shakti Kumar
On Fri, 19 Apr 2019 at 9:33 PM Tamara Berger  wrote:

> Hi Python-List,
>
> What code can I use to break out of a program completely, and not just out
> of a loop?


import sys
sys.exit()

Should do your work.

>

I wrote code with 3 conditions for saving for a downpayment. The
> first addresses cases that don't meet the minimum condition; i.e., enough
> money to save for a downpayment within the allotted time. It has its own
> print line, but also executes the irrelevant print lines for the other two
> conditions.


However anyone would suggest to put the prints in the proper if else block
rather than going for an exit.

>

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


Thanks,
Shakti.

> 
>
-- 
Sent from Shakti’s iPhone
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to determine list max without itertools

2019-04-19 Thread Rob Gaddi

On 4/19/19 12:23 AM, Sayth Renshaw wrote:

On Friday, 19 April 2019 17:01:33 UTC+10, Sayth Renshaw  wrote:

Set the first item in the list as the current largest.
 Compare each subsequent integer to the first.
 if this element is larger, set integer.


def maxitwo(listarg):
 myMax = listarg[0]
 for item in listarg:
 if item > myMax:
 myMax = item

 return myMax

Sayth



When you understand what it is you intend to write (barring DL Neil's 
comments), and THEN write it, you write the correct thing.  Thus endith 
the lesson.


--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list


RE: break out of a program

2019-04-19 Thread David Raymond
The normal way of saying you want to exit the whole program is with sys.exit()
https://docs.python.org/3.7/library/sys.html#sys.exit

You can optionally give it the return code/exit status you want the interpreter 
to give to the OS when it exits.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Tamara Berger
Sent: Friday, April 19, 2019 10:38 AM
To: python-list@python.org
Subject: 

Hi Python-List,

What code can I use to break out of a program completely, and not just out
of a loop? I wrote code with 3 conditions for saving for a downpayment. The
first addresses cases that don't meet the minimum condition; i.e., enough
money to save for a downpayment within the allotted time. It has its own
print line, but also executes the irrelevant print lines for the other two
conditions.

Thanks,
Tamara
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: (no subject)

2019-04-19 Thread Luuk

On 19-4-2019 16:37, Tamara Berger wrote:

Hi Python-List,

What code can I use to break out of a program completely, and not just out
of a loop? I wrote code with 3 conditions for saving for a downpayment. The
first addresses cases that don't meet the minimum condition; i.e., enough
money to save for a downpayment within the allotted time. It has its own
print line, but also executes the irrelevant print lines for the other two
conditions.

Thanks,
Tamara



cond1 = 1;
cond2 = 1;
cond3 = 1;

if cond1:
  if cond2:
if cond3:
  print("All OK")
else:
  print("cond3 NOK")
  else:
print("cond2 NOK")
else:
  print("cond1 NOK")



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


Re: (no subject)

2019-04-19 Thread Luuk

On 19-4-2019 16:37, Tamara Berger wrote:

Hi Python-List,

What code can I use to break out of a program completely, and not just out
of a loop? I wrote code with 3 conditions for saving for a downpayment. The
first addresses cases that don't meet the minimum condition; i.e., enough
money to save for a downpayment within the allotted time. It has its own
print line, but also executes the irrelevant print lines for the other two
conditions.

Thanks,
Tamara



cond1 = 1;
cond2 = 1;
cond3 = 1;

if cond1:
  if cond2:
if cond3:
  print("All OK")
else:
  print("cond3 NOK")
  else:
print("cond2 NOK")
else:
  print("cond1 NOK")



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


[no subject]

2019-04-19 Thread Tamara Berger
Hi Python-List,

What code can I use to break out of a program completely, and not just out
of a loop? I wrote code with 3 conditions for saving for a downpayment. The
first addresses cases that don't meet the minimum condition; i.e., enough
money to save for a downpayment within the allotted time. It has its own
print line, but also executes the irrelevant print lines for the other two
conditions.

Thanks,
Tamara
-- 
https://mail.python.org/mailman/listinfo/python-list


Questions on Instance methods

2019-04-19 Thread Arup Rakshit
>When an instance method object is created by retrieving a class method 
> object from a class or instance, its __self__ attribute is the class itself, 
> and its __func__ attribute is the function object underlying the class method.

Here I have 2 questions:

1. How do you create an instance method object from a class method object by 
using either the class or the instance?
2. Why in both cases the __self__ is set to Class only?
3. Would you give me examples also while explaining this?

>   When an instance method object is derived from a class method object, the 
> “class instance” stored in __self__ will actually be the class itself, so 
> that calling either x.f(1) or C.f(1) is equivalent to calling f(C,1) where f 
> is the underlying function.

Here x is an instance of C. Would you give me an example to illustrate why " 
x.f(1) or C.f(1) is equivalent to calling f(C,1)” ?

Quotes are taken from 
https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy 
under Callable types -> User-defined functions / Instance methods

Thanks,

Arup Rakshit
a...@zeit.io



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


Re: Question regarding the __kwdefaults__ output being None

2019-04-19 Thread Arup Rakshit
Hi Peter,


Thanks for explaining it. Beautiful.


Thanks,

Arup Rakshit
a...@zeit.io



> On 19-Apr-2019, at 5:53 PM, Peter Otten <__pete...@web.de> wrote:
> 
> Arup Rakshit wrote:
> 
>> I have a very basic function.
>> 
>> def greet(name, msg = "Good morning!"):
>>   """
>>   This function greets to
>>   the person with the
>>   provided message.
>> 
>>   If message is not provided,
>>   it defaults to "Good
>>   morning!"
>>   """
>> 
>>   print("Hello",name + ', ' + msg)
>> 
>> Now when I am calling __kwdefaults__ on the function I am getting None.
>> 
>> 3.7.3 (default, Mar 27 2019, 09:23:15)
>> [Clang 10.0.1 (clang-1001.0.46.3)]
>> Python Type "help", "copyright", "credits" or "license" for more
>> information. from python_methods import greet
>> greet("Kate")
>> Hello Kate, Good morning!
>> greet("Bruce","How do you do?")
>> Hello Bruce, How do you do?
>> print(greet.__kwdefaults__)
>> None
>> 
>> What am I missing here? Should not I get {“msg”: “Good morning!”} ?
> 
> There are defaults for arguments that can be either positional or passed as 
> a keyword -- and there are keyword-only arguments. __kwdefaults__ is used 
> for keyword-only arguments, other defaults are stored in __defaults__:
> 
 def f(a, b="kw", *, c="kwonly"): pass
> ... 
 f.__kwdefaults__
> {'c': 'kwonly'}
 f.__defaults__
> ('kw',)
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: Function to determine list max without itertools

2019-04-19 Thread Ben Bacarisse
Sayth Renshaw  writes:

>> Now, what happens when the code is tested with various (different) sets 
>> of test-data?
>> (remember the last question from my previous msg!?)
>> 
> It fails on any list entry that isn't a float or an int, giving a TypeError.
>
>> Once you are happy with the various test-runs, do you have any ideas for 
>> making the code more efficient?
>> -- 
>> Regards =dn
>
> Efficient No, but resistant to error I can buy using try except to
> pass through TypeErrors.
>
> def max_try(listarg):
> myMax = listarg[0]
> try:
> for item in listarg:
> if item > myMax:
> myMax = item
> except TypeError:
> print(f'Only numbers are supported, this entry "{item}" was not')
> pass
>
> return myMax
>
> Thanks.

'pass' does not do what you think!  To re-raise the exception, use
'raise'.  'pass' is a null statement that does nothing.

But catching an exception just to print a message from a utility
function like this is not really a good idea.  It would be annoying to
many used of this function whilst adding little value over and above
what the traceback will show anyway.

There is another error case where you /could/ add a little value by
catching it in your function and raising a more appropriate exception.
What test cases have you used so far?

> PS Since I am going through the list fully the only optimisation I can
> think of a generator to feed it seems sort of redundant. Unless of
> course it was a list of 10,000 numbers then maybe its useful.

I'm not sure what was meant here.  I can think of one micro-optimisation
but I'd want to test to see if really makes and difference.

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


Re: Question regarding the __kwdefaults__ output being None

2019-04-19 Thread Peter Otten
Arup Rakshit wrote:

> I have a very basic function.
> 
> def greet(name, msg = "Good morning!"):
>"""
>This function greets to
>the person with the
>provided message.
> 
>If message is not provided,
>it defaults to "Good
>morning!"
>"""
> 
>print("Hello",name + ', ' + msg)
> 
> Now when I am calling __kwdefaults__ on the function I am getting None.
> 
> 3.7.3 (default, Mar 27 2019, 09:23:15)
> [Clang 10.0.1 (clang-1001.0.46.3)]
> Python Type "help", "copyright", "credits" or "license" for more
> information. from python_methods import greet
> greet("Kate")
> Hello Kate, Good morning!
> greet("Bruce","How do you do?")
> Hello Bruce, How do you do?
> print(greet.__kwdefaults__)
> None
> 
> What am I missing here? Should not I get {“msg”: “Good morning!”} ?

There are defaults for arguments that can be either positional or passed as 
a keyword -- and there are keyword-only arguments. __kwdefaults__ is used 
for keyword-only arguments, other defaults are stored in __defaults__:

>>> def f(a, b="kw", *, c="kwonly"): pass
... 
>>> f.__kwdefaults__
{'c': 'kwonly'}
>>> f.__defaults__
('kw',)


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


Re: Function to determine list max without itertools

2019-04-19 Thread Dan Sommers

On 4/19/19 4:01 AM, Sayth Renshaw wrote:


Now, what happens when the code is tested with various (different) sets 
of test-data?

(remember the last question from my previous msg!?)


It fails on any list entry that isn't a float or an int, giving a TypeError.


What if the *first* entry isn't a float or an int?  For
instance, what if the list is ['a', 'b', 'c']?  What about
['a', 'b', 3]?

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


Question regarding the __kwdefaults__ output being None

2019-04-19 Thread Arup Rakshit
I have a very basic function.

def greet(name, msg = "Good morning!"):
   """
   This function greets to
   the person with the
   provided message.

   If message is not provided,
   it defaults to "Good
   morning!"
   """

   print("Hello",name + ', ' + msg)

Now when I am calling __kwdefaults__ on the function I am getting None.

3.7.3 (default, Mar 27 2019, 09:23:15) 
[Clang 10.0.1 (clang-1001.0.46.3)]
Python Type "help", "copyright", "credits" or "license" for more information.
from python_methods import greet
greet("Kate")
Hello Kate, Good morning!
greet("Bruce","How do you do?")
Hello Bruce, How do you do?
print(greet.__kwdefaults__)
None

What am I missing here? Should not I get {“msg”: “Good morning!”} ?


Thanks,

Arup Rakshit
a...@zeit.io



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


Re: Function to determine list max without itertools

2019-04-19 Thread Sayth Renshaw


> Now, what happens when the code is tested with various (different) sets 
> of test-data?
> (remember the last question from my previous msg!?)
> 
It fails on any list entry that isn't a float or an int, giving a TypeError.

> Once you are happy with the various test-runs, do you have any ideas for 
> making the code more efficient?
> -- 
> Regards =dn

Efficient No, but resistant to error I can buy using try except to pass through 
TypeErrors.

def max_try(listarg):
myMax = listarg[0]
try:
for item in listarg:
if item > myMax:
myMax = item
except TypeError:
print(f'Only numbers are supported, this entry "{item}" was not')
pass

return myMax


Thanks.

PS Since I am going through the list fully the only optimisation I can think of 
a generator to feed it seems sort of redundant. Unless of course it was a list 
of 10,000 numbers then maybe its useful. 

Maybe a generator with a condition that if list length is greater than 500 to 
chunk it into 250 lengths. 

But then If we go along this path then you could say if every other task in an 
application waited on it could be very important so then async await might be 
an option.

Sayth

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


Re: Function to determine list max without itertools

2019-04-19 Thread DL Neil

> On 19/04/19 7:23 PM, Sayth Renshaw wrote:


In English:

  Set the first item in the list as the current largest. 
Compare each subsequent integer to the first.
if this element is larger, set integer. 



Criticism: (because this does NOT match the code, below!)
- should the algorithm "Compare each subsequent integer to the first" or 
is the comparison with 'the current largest', ie 'the largest so-far'?


NB IIRC this was (likely) why it was suggested that you explain the 
method in English, first!



In code:


def maxitwo(listarg):
 myMax = listarg[0]
 for item in listarg:
 if item > myMax:
 myMax = item

 return myMax



Well done!

Now, what happens when the code is tested with various (different) sets 
of test-data?

(remember the last question from my previous msg!?)


Once you are happy with the various test-runs, do you have any ideas for 
making the code more efficient?

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Function to determine list max without itertools

2019-04-19 Thread Sayth Renshaw
On Friday, 19 April 2019 17:01:33 UTC+10, Sayth Renshaw  wrote:
> Set the first item in the list as the current largest. 
> Compare each subsequent integer to the first.
> if this element is larger, set integer.

def maxitwo(listarg):
myMax = listarg[0]
for item in listarg:
if item > myMax:
myMax = item

return myMax

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


Re: Function to determine list max without itertools

2019-04-19 Thread Sayth Renshaw



  Set the first item in the list as the current largest. 
Compare each subsequent integer to the first.
if this element is larger, set integer. 
-- 
https://mail.python.org/mailman/listinfo/python-list