Re: Why won't the run-time error reporter point to the error position with a caret like the syntax error reporter does? It knows exactly where the error is.

2019-08-01 Thread Chris Angelico
On Thu, Aug 1, 2019 at 4:34 PM Terry Reedy  wrote:
>
> On 7/31/2019 11:19 PM, jsals...@gmail.com wrote:
> > Honestly this is the only thing in over half a decade of daily python use 
> > which has disappointed me enough to want to ask the devs:
> >
>  print(1/)
> >File "", line 1
> >  print(1/)
> >  ^
> > SyntaxError: invalid syntax
>
> SyntaxErrors mostly come from the parser, occasionally from the
> compiler, both of which have access to line and column.
>
>  print(1/1, 1/0, 1/1)
> > Traceback (most recent call last):
> >File "", line 1, in 
> > ZeroDivisionError: division by zero
>
> This comes from the runtime engine, which only has access to line
> numbers, and even line numbers lie when a statement spans multiple
> lines.  In CPython, the runtime engine is executing bytecodes, and
> bytecode do not correspond to column numbers.  In something like
> "a + b / (2 * c + d//3)", if the denominator (which could be on multiple
> lines) is 0, where should a caret point?
>

Probably to the slash, since it's the division operation that threw
the error. I don't think it would be all that useful, but it would at
least be sane and logical.

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


newbie question

2019-08-01 Thread Sidney Langweil
A Python script invokes a function in another file in the same directory.

I would like to invoke that function without the need for an import.

I think I read that having an empty __init__.py is sufficient.  But it does not 
seem to work for me.

I'm sure this is obvious to many of you.  Thanks in advance for your help.

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


Re: newbie question

2019-08-01 Thread Calvin Spealman
Sorry, but you can't. If you have two python modules, neither has access to
things in the other without an import.

That's the whole point of an import.

On Thu, Aug 1, 2019 at 10:30 AM Sidney Langweil 
wrote:

> A Python script invokes a function in another file in the same directory.
>
> I would like to invoke that function without the need for an import.
>
> I think I read that having an empty __init__.py is sufficient.  But it
> does not seem to work for me.
>
> I'm sure this is obvious to many of you.  Thanks in advance for your help.
>
> Sid
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

cspea...@redhat.com  M: +1.336.210.5107
[image: https://red.ht/sig] 
TRIED. TESTED. TRUSTED. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: newbie question

2019-08-01 Thread Sidney Langweil
On Thursday, August 1, 2019 at 7:57:31 AM UTC-7, Calvin Spealman wrote:
> Sorry, but you can't. If you have two python modules, neither has access to
> things in the other without an import.
> 
> That's the whole point of an import.
> 
> On Thu, Aug 1, 2019 at 10:30 AM Sidney Langweil 
> wrote:
> 
> > A Python script invokes a function in another file in the same directory.
> >
> > I would like to invoke that function without the need for an import.
> >
> > I think I read that having an empty __init__.py is sufficient.  But it
> > does not seem to work for me.
> >
> > I'm sure this is obvious to many of you.  Thanks in advance for your help.
> >
> > Sid
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> 
> 
> -- 
> 
> CALVIN SPEALMAN
> 
> SENIOR QUALITY ENGINEER
> 
> cspea...@redhat.com  M: +1.336.210.5107
> [image: https://red.ht/sig] 
> TRIED. TESTED. TRUSTED. 

Thank you.  As long as using 'import' is correct in this case, I do not mind 
inserting the extra line(s).

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


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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Create Logging module

2019-08-01 Thread Sinardy Xing
Sorry for spamming this is suppose send to tutor-ow...@python.org

On Thu, Aug 1, 2019 at 5:08 PM Sinardy Xing  wrote:

> 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
>
-- 
https://mail.python.org/mailman/listinfo/python-list


help with tkinter

2019-08-01 Thread adam kabbara
Hello I am having trouble with tkinter when I enter the command from tkinter 
import* I get an error message


Sent from Mail for Windows 10

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


Re: help with tkinter

2019-08-01 Thread Rhodri James

Hi there, Adam!

On 01/08/2019 14:31, adam kabbara wrote:

Hello I am having trouble with tkinter when I enter the command from tkinter 
import* I get an error message


What *exactly* did you type?  Please cut-and-paste from your 
console/editor into the email, don't just retype it.  If you did 
literally type "from tkinter import*" then Python is probably 
complaining that it doesn't know what "import*" means because you missed 
out a space!


Also, what *exactly* was the error message?  Again, please 
cut-and-paste.  Python usually gives you a pretty good indication of 
what you did wrong; if you show us, we can help you get used to 
interpreting it for yourself.


Happy coding!


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: help with tkinter

2019-08-01 Thread Chris Angelico
On Fri, Aug 2, 2019 at 2:30 AM Rhodri James  wrote:
>
> Hi there, Adam!
>
> On 01/08/2019 14:31, adam kabbara wrote:
> > Hello I am having trouble with tkinter when I enter the command from 
> > tkinter import* I get an error message
>
> What *exactly* did you type?  Please cut-and-paste from your
> console/editor into the email, don't just retype it.  If you did
> literally type "from tkinter import*" then Python is probably
> complaining that it doesn't know what "import*" means because you missed
> out a space!

Actually, Python's fine with that, but the principle is still valid:
be specific about what you entered and what Python replied.

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


Re: help with tkinter

2019-08-01 Thread Rhodri James

On 01/08/2019 19:06, Chris Angelico wrote:

On Fri, Aug 2, 2019 at 2:30 AM Rhodri James  wrote:


Hi there, Adam!

On 01/08/2019 14:31, adam kabbara wrote:

Hello I am having trouble with tkinter when I enter the command from tkinter 
import* I get an error message


What *exactly* did you type?  Please cut-and-paste from your
console/editor into the email, don't just retype it.  If you did
literally type "from tkinter import*" then Python is probably
complaining that it doesn't know what "import*" means because you missed
out a space!


Actually, Python's fine with that, but the principle is still valid:
be specific about what you entered and what Python replied.


Fair enough.  I should try out "obvious syntax errors" before I declare 
them as such.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


PEP: add a `no` keyword as an alias for `not`

2019-08-01 Thread Daniel Okey-Okoro
I think that adding a `no` keyword as an alias for `not` would make for
more readable, simple, pythonic code.

Take the below:

```
if not val:
  do_thing_because_value_is_falsy()
```

could be (is actually understood as):

```
if no val:
   do_thing_because_value_is_falsy()
```

I think this PEP is a work-around for an underlying subtle issue with how
the `not` operator is used.

It has two use-cases:

1. as a NOT gate for producing opposite boolean values

```
opposite = not regular
```

2. as a sort of  ".is_falsy()"  checker; when used with an if statement.

like the first example.


This PEP would make the difference between the two usecases explicit.

Thoughts?

Best Intentions,
Daniel Okey-Okoro.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PEP: add a `no` keyword as an alias for `not`

2019-08-01 Thread Calvin Spealman
I think they actually read like they would mean slightly different things,
which would make them existing as aliases confusing.

I read `if not val` as "If val isn't true" but i would read `if no val` as
"if val does not exist"

On Thu, Aug 1, 2019 at 4:07 PM Daniel Okey-Okoro 
wrote:

> I think that adding a `no` keyword as an alias for `not` would make for
> more readable, simple, pythonic code.
>
> Take the below:
>
> ```
> if not val:
>   do_thing_because_value_is_falsy()
> ```
>
> could be (is actually understood as):
>
> ```
> if no val:
>do_thing_because_value_is_falsy()
> ```
>
> I think this PEP is a work-around for an underlying subtle issue with how
> the `not` operator is used.
>
> It has two use-cases:
>
> 1. as a NOT gate for producing opposite boolean values
>
> ```
> opposite = not regular
> ```
>
> 2. as a sort of  ".is_falsy()"  checker; when used with an if statement.
>
> like the first example.
>
>
> This PEP would make the difference between the two usecases explicit.
>
> Thoughts?
>
> Best Intentions,
> Daniel Okey-Okoro.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

cspea...@redhat.com  M: +1.336.210.5107
[image: https://red.ht/sig] 
TRIED. TESTED. TRUSTED. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Fwd: PEP: add a `no` keyword as an alias for `not`

2019-08-01 Thread Daniel Okey-Okoro
-- Forwarded message -
From: Daniel Okey-Okoro 
Date: Thu, Aug 1, 2019 at 1:24 PM
Subject: Re: PEP: add a `no` keyword as an alias for `not`
To: Calvin Spealman 


Good point Calvin,

But in many cases, when people write `if not val` they're checking if the
val is `None`

i.e. `if val is None`, `if val == None`

(so in effect it's kind of like they're checking if "something isn't there")

I just did a code review. The idea popped in my head after reading code
that behaved this way.
---
Plus, I'm suggesting an alias.

In some usecases it would be better to just use `if not val`.

i.e. in cases where you are strictly doing True/False assessments not
Falsey ( is_empty() ) assessments e.g. None, [], "", 0
```


Best Intentions,
Daniel Okey-Okoro

On Thu, Aug 1, 2019, 1:10 PM Calvin Spealman  wrote:

> I think they actually read like they would mean slightly different things,
> which would make them existing as aliases confusing.
>
> I read `if not val` as "If val isn't true" but i would read `if no val` as
> "if val does not exist"
>
> On Thu, Aug 1, 2019 at 4:07 PM Daniel Okey-Okoro <
> danielokeyok...@gmail.com> wrote:
>
>> I think that adding a `no` keyword as an alias for `not` would make for
>> more readable, simple, pythonic code.
>>
>> Take the below:
>>
>> ```
>> if not val:
>>   do_thing_because_value_is_falsy()
>> ```
>>
>> could be (is actually understood as):
>>
>> ```
>> if no val:
>>do_thing_because_value_is_falsy()
>> ```
>>
>> I think this PEP is a work-around for an underlying subtle issue with how
>> the `not` operator is used.
>>
>> It has two use-cases:
>>
>> 1. as a NOT gate for producing opposite boolean values
>>
>> ```
>> opposite = not regular
>> ```
>>
>> 2. as a sort of  ".is_falsy()"  checker; when used with an if statement.
>>
>> like the first example.
>>
>>
>> This PEP would make the difference between the two usecases explicit.
>>
>> Thoughts?
>>
>> Best Intentions,
>> Daniel Okey-Okoro.
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
>
> --
>
> CALVIN SPEALMAN
>
> SENIOR QUALITY ENGINEER
>
> cspea...@redhat.com  M: +1.336.210.5107
> [image: https://red.ht/sig] 
> TRIED. TESTED. TRUSTED. 
>


-- 
Best Intentions,
Daniel Okey-Okoro.
-- 
https://mail.python.org/mailman/listinfo/python-list


Fwd: [Python-ideas] Re: PEP: add a `no` keyword as an alias for `not`

2019-08-01 Thread Daniel Okey-Okoro
-- Forwarded message -
From: Daniel Okey-Okoro 
Date: Thu, Aug 1, 2019 at 1:37 PM
Subject: Re: [Python-ideas] Re: PEP: add a `no` keyword as an alias for
`not`
To: Chris Angelico 


> not a strong enough justification for breaking any code that uses "no" in
any other way.

This is a very crucial point I didn't consider.



What if we could lexically limit it to `if no {{everything else in the if
block}}`?

I think that would sufficiently protect us from unintentionally breaking
people's code.

---

> The bar for creating new keywords is extremely high

I'm grateful that it is. I think python is awesome. And I trust the
community/council to keep it awesome. If the idea doesn't make sense to
most people then I'm happy with it never getting picked up.

Just sharing an idea.

On Thu, Aug 1, 2019 at 1:12 PM Chris Angelico  wrote:

> On Fri, Aug 2, 2019 at 6:09 AM Daniel Okey-Okoro
>  wrote:
> >
> > I think that adding a `no` keyword as an alias for `not` would make for
> > more readable, simple, pythonic code.
>
> The bar for creating new keywords is extremely high. Simply making
> your code slightly more like English is not a strong enough
> justification for breaking any code that uses "no" in any other way.
>
> ChrisA
> ___
> Python-ideas mailing list -- python-id...@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-id...@python.org/message/DJUKCQH3RA6HKCYUHOXBKDRCULJKB6LT/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Best Intentions,
Daniel Okey-Okoro.


-- 
Best Intentions,
Daniel Okey-Okoro.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help with tkinter

2019-08-01 Thread Wildman via Python-list
On Thu, 01 Aug 2019 16:31:02 +0300, adam kabbara wrote:

> Hello I am having trouble with tkinter when I enter the command from tkinter 
> import* I get an error message
> 

What was the error and what version of Python are
you using?

For Python 2 you need... from Tkinter import*

-- 
 GNU/Linux user #557453
"There are only 10 types of people in the world...
those who understand Binary and those who don't."
  -Spike
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fwd: PEP: add a `no` keyword as an alias for `not`

2019-08-01 Thread Karsten Hilbert
On Thu, Aug 01, 2019 at 01:25:21PM -0700, Daniel Okey-Okoro wrote:

> Good point Calvin,
> 
> But in many cases, when people write `if not val` they're checking if the
> val is `None`
>
> i.e. `if val is None`, `if val == None`
>
> (so in effect it's kind of like they're checking if "something isn't there")

No. Something being None does not mean something isn't there.

Something isn't there if something isn't there:

hasattr(object_or_scope_to_check, 'name_of_attribute')

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fwd: PEP: add a `no` keyword as an alias for `not`

2019-08-01 Thread Grant Edwards
On 2019-08-01, Daniel Okey-Okoro  wrote:

> But in many cases, when people write `if not val` they're checking if the
> val is `None`
>
> i.e. `if val is None`, `if val == None`

If they want to know if val is None, then they should be typing 'val
is None'.  IMO, it's not an improvement to let them type 'no val'
instead of 'not val' when what they really mean is 'val is None'.

> (so in effect it's kind of like they're checking if "something isn't
> there")

Except there _is_ something there.  It just has a 'false' value in the
case of 'not val' or it's the None singleton object in the 'val is
None'.

Something not being there is a whole different thing...

-- 
Grant Edwards   grant.b.edwardsYow! My mind is making
  at   ashtrays in Dayton ...
  gmail.com

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


Re: PEP: add a `no` keyword as an alias for `not`

2019-08-01 Thread Ethan Furman

On 08/01/2019 01:06 PM, Daniel Okey-Okoro wrote:


[an idea]


Two things:

1) please do not cross-post.

2) a PEP is a very particular thing* -- please do not say you have one unless 
you do. ;)

--
~Ethan~


* https://www.python.org/dev/peps/pep-0001/
--
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a simple way to wrap a built-in function for the whole package?

2019-08-01 Thread Cameron Simpson

On 31Jul2019 19:16, Jach Fong  wrote:

I get a package from Pypi. The package has many modules using built-in open() 
function. I like to redefine all the open() there with the default encoding 
'utf-8', but not for code outside the package. Maybe I can put my def statement 
at the beginning of every module of this package, but just wondering is there a 
simple way of doing it?


I would define something like this in the __init__.py file of the 
package (or in some utils.py file in the package):


 def open8(path):
   return open(path, encoding='utf-8')

and just:

 from . import open8

and use "open8" throughout the code instead of "open"?

What not alias "open"? Because I would want it evident that in this code 
we're doing a slightly special flavour of open. By using a distinct name 
I avoid confusion about the unusual semantics.


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


Re: Is there a simple way to wrap a built-in function for the whole package?

2019-08-01 Thread jfong
Cameron Simpson於 2019年8月2日星期五 UTC+8上午6時25分00秒寫道:
> On 31Jul2019 19:16, Jach Fong  wrote:
> >I get a package from Pypi. The package has many modules using built-in 
> >open() function. I like to redefine all the open() there with the default 
> >encoding 'utf-8', but not for code outside the package. Maybe I can put my 
> >def statement at the beginning of every module of this package, but just 
> >wondering is there a simple way of doing it?
> 
> I would define something like this in the __init__.py file of the 
> package (or in some utils.py file in the package):
> 
>   def open8(path):
> return open(path, encoding='utf-8')
> 
> and just:
> 
>   from . import open8
> 
> and use "open8" throughout the code instead of "open"?
> 
> What not alias "open"? Because I would want it evident that in this code 
> we're doing a slightly special flavour of open. By using a distinct name 
> I avoid confusion about the unusual semantics.
> 
> Cheers,
> Cameron Simpson 

Instead of changing open() to open8() throughout the code, I prefer change it  
directly to open(encoding='utf-8'). Anyway, using "from . import open" is a 
good idea, thank you.

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