[Tutor] Difference between decorator and inheritance

2019-08-01 Thread Gursimran Maken
Hi,

Anyone could please let me know the difference between decorators and
inheritance in python.

Both are required to add additional functionality to a method then why are
we having 2 separate things in python for doing same kind of work.

Thank you,
Gursimran
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between decorator and inheritance

2019-08-01 Thread Alan Gauld via Tutor
On 31/07/2019 18:57, Gursimran Maken wrote:

> Anyone could please let me know the difference between decorators and
> inheritance in python.
> 
> Both are required to add additional functionality to a method then why are
> we having 2 separate things in python for doing same kind of work.

Inheritance and decorators can both achieve similar results in that
they provide a mechanism to alter the effect of a method, but they
work very differently. In particular, decorators can be used outside
of a class, on a normal function. They also work across multiple
different classes whereas works within a single heirarchy to create
a subtype structure (but see Mixins below).

Also, inheritance is used for much more than modifying methods.
You can add new methods and attributes and it also provides
conceptual structure to your classes as well as being a tool for
software reuse (probably its least useful, and most abused,
function). Inheritance in other OOP languages is used for
many other things too, especially as the mechanism that
enables polymorphism, but that is less significant in Python.

Where there is considerable overlap is in the use of mixin
classes. Mixins are a form of inheritance which allow
modification of methods in a way quite similar (from a
users perspective) to decorators. The underlying mechanisms
are different and decorators are more elegant, but the end
result is similar.

I'm not sure how much technical detail you wanted, so I'll
leave it thee, if you want to dig into the inner mechanisms
then ask for more details.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Create Logging module

2019-08-01 Thread Sinardy Xing
Hi,

I am learning to create python logging.

My goal is to create a logging module where I can use as decorator in my
main app

following is the logging code

 start here---

import logging

#DEBUG: Detailed information, typically of interest only when
diagnosing problems.
#INFO : Confirmation that things are working as expected.
#WARNING  (default): An indication that something unexpected happened, or
indicative of some problem in the near future
# (e.g. 'disk space low'). The software is still working as
expected.
#ERROR: Due to a more serious problem, the software has not been able
to perform some function.
#CRITICAL :A serious error, indicating that the program itself may be
unable to continue running.

from functools import wraps

def logme(func_to_log):
import logging

#Without getLogger name it will log all in root
logger = logging.getLogger(__name__)

#Check log level within understanable parameter, set to INFO if is not
 permitable value
def check_log_level(logleveltocheck):
if any(logleveltocheck.upper() in lf for lf in ['DEBUG',
'INFO', 'WARNING', 'ERROR', 'CRITICAL']):
return logleveltocheck.upper()
else:
return 'INFO'

log_file_level='INFO' #check_log_level('info')
log_console_level='INFO' #check_log_level('info')

#root level
logger.setLevel('INFO')

formatter = logging.Formatter('%(asctime)s :: %(name)s :: %(levelname)s
:: %(message)s')

#Read log file from parameter
logfile='mylogfile.log'
file_handler = logging.FileHandler(logfile)
file_handler.setLevel(log_file_level)
file_handler.setFormatter(formatter)

stream_handler = logging.StreamHandler()
stream_handler.setLevel(log_console_level)
stream_handler.setFormatter(formatter)

logger.addHandler()
logger.addHandler(stream_handler)

#this wraps is to make sure we are returning func_to_log instead of
wrapper
@wraps(func_to_log)
def wrapper(*args, **kwargs):
logger.info('Ran with args: {}, and kwargs: {}'.format(args,
kwargs))
return func_to_log(*args, **kwargs)

return wrapper


def timer(func_to_log):
import time

#this wraps is to make sure we are returning func_to_log instead of
wrapper
@wraps(func_to_log)
def wrapper(*args, **kwargs):
t1 = time.time()
result = func_to_log(*args, **kwargs)
t2 = time.time() - t1
print('{} ran in {} sec'.format(func_to_log.__name__, t2))
return result

--- end here---


following is my main app

-- start here--
from loggingme import logme

def say_hello(name, age):
print('Hello {}, I am {}'.format(name, age))

#say_hello=logme(say_hello('Sinardy'))
@logme
say_hello('Tonny', 8)

--- end here---


I have error look like in the wrapper.

Can someone point to me where is the issue or is this the correct way to
create logging module?

PS: above code with python 3.7.4

Thank you.

regards,
C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create Logging module

2019-08-01 Thread Steven D'Aprano
On Thu, Aug 01, 2019 at 05:11:04PM +0800, Sinardy Xing wrote:

> I have error look like in the wrapper.
> 
> Can someone point to me where is the issue

No, but you can.

When the error occurs, Python will print a traceback containing a list 
of the lines of code being executed, and the final error. You should 
read that error, especially the last line, and it will tell you the line 
of code that failed and give you a clue why it failed.

We can help you if you copy and paste the full traceback, starting with 
the line "Traceback..." to the end.

Don't take a screen shot or photo and send that, instead copy and paste 
the text into your email.

(P.S. please reply to the mailing list, not to me personally.)


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create Logging module

2019-08-01 Thread Alan Gauld via Tutor
On 01/08/2019 10:11, Sinardy Xing wrote:

>  start here---
> 
> import logging
> 
>  ..snip...


> from functools import wraps
> 
> def logme(func_to_log):
> import logging

You don't need the import, it's already done in the first line.


> #Check log level within understanable parameter, set to INFO if is not
>  permitable value
> def check_log_level(logleveltocheck):

This looks like an indentation error?
It should be at the same level as the import statement.

> if any(logleveltocheck.upper() in lf for lf in ['DEBUG',
> 'INFO', 'WARNING', 'ERROR', 'CRITICAL']):
> return logleveltocheck.upper()

Are you sure that is what you want? It seems very complicated unless you
are allowing the user to supply an abbreviated form. Otherwise

if logleveltocheck.upper() in ['DEBUG', 'INFO', 'WARNING',
   'ERROR', 'CRITICAL']:
return logleveltocheck.upper()

might be easier?

> else
> return 'INFO'
> 
> log_file_level='INFO' #check_log_level('info')
> log_console_level='INFO' #check_log_level('info')
> 
> #root level
> logger.setLevel('INFO')

I'm not sure what this is supposed to be doing!

> formatter = logging.Formatter('%(asctime)s :: %(name)s :: %(levelname)s
> :: %(message)s')
> 
> #Read log file from parameter
> logfile='mylogfile.log'
> file_handler = logging.FileHandler(logfile)
> file_handler.setLevel(log_file_level)
> file_handler.setFormatter(formatter)
> 
> stream_handler = logging.StreamHandler()
> stream_handler.setLevel(log_console_level)
> stream_handler.setFormatter(formatter)
> 
> logger.addHandler()
> logger.addHandler(stream_handler)
> 
> #this wraps is to make sure we are returning func_to_log instead of
> wrapper
> @wraps(func_to_log)
> def wrapper(*args, **kwargs):
> logger.info('Ran with args: {}, and kwargs: {}'.format(args,
> kwargs))
> return func_to_log(*args, **kwargs)
> 
> return wrapper

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create Logging module

2019-08-01 Thread Peter Otten
Sinardy Xing wrote:

> following is my main app
> 
> -- start here--
> from loggingme import logme
> 
> def say_hello(name, age):
> print('Hello {}, I am {}'.format(name, age))
> 
> #say_hello=logme(say_hello('Sinardy'))
> @logme
> say_hello('Tonny', 8)

Isn't this a SyntaxError? You can decorate functions, not function calls:

When Python finds a syntax error in your main script it won't proceed to run 
it and thus the bugs in modules that would be imported if the code were 
executed don't matter at this point.

Try

@logme
def say_hello(name, age):
print('Hello {}, I am {}'.format(name, age))

say_hello('Tonny', 8)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python and Django dev available

2019-08-01 Thread Sithembewena L. Dube
Hi everyone,

I'm a developer with just over 10 years' experience under my belt. I've
worked with many languages and frameworks, including the popular Django web
framework.

I'm currently in the market for remote opportunities. Please reply if
looking for a resource. Serious enquiries only please.


Kind regards,
Sithembewena

*Sent with Shift
*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create Logging module

2019-08-01 Thread Sinardy Xing
Hi Steven,

Thanks for your reply, I was copy and paste the code in the email as a text.
I dont know why it becoming photo or screen shot when you view it ?

When I run the module individually it is no error only when I use as
decorator I have error.



$ cat mainapp.py

from loggingme import logme


def say_hello(name, age):

print('Hello {}, I am {}'.format(name, age))


@logme

say_hello('Tonny', 8)



$ python3 mainapp.py

  File "mainapp.py", line 8

say_hello('Tonny', 8)

^

SyntaxError: invalid syntax

On Thu, Aug 1, 2019 at 8:42 PM Steven D'Aprano  wrote:

> On Thu, Aug 01, 2019 at 05:11:04PM +0800, Sinardy Xing wrote:
>
> > I have error look like in the wrapper.
> >
> > Can someone point to me where is the issue
>
> No, but you can.
>
> When the error occurs, Python will print a traceback containing a list
> of the lines of code being executed, and the final error. You should
> read that error, especially the last line, and it will tell you the line
> of code that failed and give you a clue why it failed.
>
> We can help you if you copy and paste the full traceback, starting with
> the line "Traceback..." to the end.
>
> Don't take a screen shot or photo and send that, instead copy and paste
> the text into your email.
>
> (P.S. please reply to the mailing list, not to me personally.)
>
>
> --
> Steven
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create Logging module

2019-08-01 Thread Sinardy Xing
Hi I solve my problem,

the decorator need to be above the function not when executed. :)

Thanks for reading.

:)



On Thu, Aug 1, 2019 at 8:42 PM Steven D'Aprano  wrote:

> On Thu, Aug 01, 2019 at 05:11:04PM +0800, Sinardy Xing wrote:
>
> > I have error look like in the wrapper.
> >
> > Can someone point to me where is the issue
>
> No, but you can.
>
> When the error occurs, Python will print a traceback containing a list
> of the lines of code being executed, and the final error. You should
> read that error, especially the last line, and it will tell you the line
> of code that failed and give you a clue why it failed.
>
> We can help you if you copy and paste the full traceback, starting with
> the line "Traceback..." to the end.
>
> Don't take a screen shot or photo and send that, instead copy and paste
> the text into your email.
>
> (P.S. please reply to the mailing list, not to me personally.)
>
>
> --
> Steven
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python code

2019-08-01 Thread Spencer Wannemacher
I'm new to python and I was trying to perform a simple one code. All that is 
included in the code is print(61). I save it as 61.py and change the directory 
before typing in python 61.py and I don't get an output. There is no error and 
the output is blank. Please let me know what I'm doing wrong. Thank you so much!

Sent from Mail for Windows 10

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python code

2019-08-01 Thread Cameron Simpson

On 01Aug2019 15:23, Spencer Wannemacher  wrote:
I'm new to python and I was trying to perform a simple one code. All 
that is included in the code is print(61). I save it as 61.py and 
change the directory before typing in python 61.py and I don't get an 
output. There is no error and the output is blank. Please let me know 
what I'm doing wrong. Thank you so much!


Can you confirm that your entire "61.py" file is this:

 print(61)

That should print a "61" on the output, as I think you expected.

Can you confirm your operating system? I'm guessing Windows, but a 
detailed description would be helpful.


Are you are typing "python 61.py" at the shell prompt (I presume 
Windows' CMD.EXE prompt)? And not to some other interface?


It is my recollection that "python" isn't a command under windows, 
there's a "py" command (I'm not on Windows here). So it may be that when 
you issue the command "python" it isn't running the Python interpreter 
but something else.


Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python code

2019-08-01 Thread David L Neil

On 2/08/19 3:23 AM, Spencer Wannemacher wrote:

I'm new to python and I was trying to perform a simple one code. All that is 
included in the code is print(61). I save it as 61.py and change the directory 
before typing in python 61.py and I don't get an output. There is no error and 
the output is blank. Please let me know what I'm doing wrong. Thank you so much!



What do you mean by "and change the directory before"?

Python will start searching for 61.py in the *current* directory!

--
Regards =dn
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python code

2019-08-01 Thread David Rock
maybe a copy/paste of your terminal session so we can see the text of the steps 
you are actually performing will give use some clues.

—
David

> On Aug 1, 2019, at 18:22, David L Neil  wrote:
> 
> On 2/08/19 3:23 AM, Spencer Wannemacher wrote:
>> I'm new to python and I was trying to perform a simple one code. All that is 
>> included in the code is print(61). I save it as 61.py and change the 
>> directory before typing in python 61.py and I don't get an output. There is 
>> no error and the output is blank. Please let me know what I'm doing wrong. 
>> Thank you so much!
> 
> 
> What do you mean by "and change the directory before"?
> 
> Python will start searching for 61.py in the *current* directory!
> 
> -- 
> Regards =dn
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] just a quick logic check if someone has two seconds

2019-08-01 Thread nathan tech
Hi there,

I wondered if someone wouldn't mind just taking two seconds to make sure 
i understand this concept:

Here is a code snippet:

import speedtest

def do-test():

  test=speedtest.Speedtest()

  test.download()

  test.upload()

  return [test.download_speed, test.upload_speed]


Now. If I was to put this into a GUI application, I was thinking of 
having it something like this:

user clicks button,

button calls function which:

1. Shows the screen which updates with test status.

2, does: results=do_test()

3. Updates the screen with the contents of results.


Here's my question:

If the user clicks the button, say, 3 times, will I have three separate 
speedtest objects?

or will the python garbage collector clean them up for me so I only have 
one, which gets cleaned when do_test returns.

Thanks for the answer in advance.

Nathan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] just a quick logic check if someone has two seconds

2019-08-01 Thread Alan Gauld via Tutor
On 01/08/2019 23:10, nathan tech wrote:

> 
> import speedtest

This is not a standard library module so I have no idea
what it does so obviously there could be magic afoot of
which I am unaware. But assuming it behaves like most
Python code...

> def do-test():
>   test=speedtest.Speedtest()
>   test.download()
>   test.upload()
>   return [test.download_speed, test.upload_speed]

test is garbage collected sat this point since it
goes out of scope and the returned values are passed
to the caller. Note that the returned values are not
part of the test object. The test attributes refer to
those values but it is the values themselves that
are returned.

> Now. If I was to put this into a GUI application, I was thinking of 
> having it something like this:

The fact it is a GUI is completely irrelevant.
There is nothing special about how a GUI calls a function.

> user clicks button,
> button calls function which:
> 
> 1. Shows the screen which updates with test status.
> 2, does: results=do_test()
> 3. Updates the screen with the contents of results.

The fact that the GUI calls this function is irrelevant.
A function gets called and performs some actions.
One of which is to call do_test(). It would be exactly the same if you
did this:

for n in range(3):
   result = do_test()
   print(result)

You still call the function repeatedly.

> If the user clicks the button, say, 3 times, will I have three separate 
> speedtest objects?

You will have created 3 separate speedtest instances and each
will have been garbage collected when do_test() terminated.
So you will have no speedtest instances left hanging around.

> or will the python garbage collector clean them up for me so I only have 
> one, which gets cleaned when do_test returns.

You only ever have one at a time during the execution of do_test().
You have a total of 3 during your programs lifetime. (or however many
times you click the button!)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor