I suppose this has already been proposed in past but couldn't find any online reference so here goes. When it comes to module constant imports I usually like being explicit it's OK with me as long as I have to do:
from resource import (RLIMIT_CORE, RLIMIT_CPU, RLIMIT_FSIZE)
Nevertheless in case the existence of certain constants depends on the platform in use I end up doing:
if hasattr(resource, "RLIMIT_MSGQUEUE"): # linux only .... import resource.RLIMIT_MSGQUEUE .... if hasattr(resource, "RLIMIT_NICE"): # linux only .... import resource.RLIMIT_NICE ....
...or worse, if for simplicity I'm willing to simply import all RLIMIT_* constants I'll have to do this:
import resource import sys for name in dir(resource): .... if name.startswith('RLIMIT_'): .... setattr(sys.modules[__name__], name, getattr(resource, name))
...or just give up and use: from resource import * ...which of course will pollute the namespace with unnecessary stuff. So why not just allow "from resource import RLIMIT_*" syntax? Another interesting variation might be:
from socket import AF_*, SOCK_* AF_INET, AF_INET6, SOCK_STREAM, SOCK_DGRAM (2, 10, 1, 2)
On the other hand mixing "*" and "common" imports would be forbidden:
from socket import AF_*, socket, File "<stdin>", line 1 from socket import AF_*, socket ^ SyntaxError: invalid syntax;
Thoughts? --- Giampaolo https://code.google.com/p/pyftpdlib/ https://code.google.com/p/psutil/ https://code.google.com/p/pysendfile/
Hm. Why not just use "import socket" and then use "socket.AF_<whatever>"? On Thu, Oct 3, 2013 at 10:09 AM, Giampaolo Rodola' <g.rodola@gmail.com>wrote:
I suppose this has already been proposed in past but couldn't find any online reference so here goes. When it comes to module constant imports I usually like being explicit it's OK with me as long as I have to do:
from resource import (RLIMIT_CORE, RLIMIT_CPU, RLIMIT_FSIZE)
Nevertheless in case the existence of certain constants depends on the platform in use I end up doing:
if hasattr(resource, "RLIMIT_MSGQUEUE"): # linux only .... import resource.RLIMIT_MSGQUEUE .... if hasattr(resource, "RLIMIT_NICE"): # linux only .... import resource.RLIMIT_NICE ....
...or worse, if for simplicity I'm willing to simply import all RLIMIT_* constants I'll have to do this:
import resource import sys for name in dir(resource): .... if name.startswith('RLIMIT_'): .... setattr(sys.modules[__name__], name, getattr(resource, name))
...or just give up and use:
from resource import *
...which of course will pollute the namespace with unnecessary stuff. So why not just allow "from resource import RLIMIT_*" syntax? Another interesting variation might be:
from socket import AF_*, SOCK_* AF_INET, AF_INET6, SOCK_STREAM, SOCK_DGRAM (2, 10, 1, 2)
On the other hand mixing "*" and "common" imports would be forbidden:
from socket import AF_*, socket, File "<stdin>", line 1 from socket import AF_*, socket ^ SyntaxError: invalid syntax;
Thoughts?
--- Giampaolo https://code.google.com/p/pyftpdlib/ https://code.google.com/p/psutil/ https://code.google.com/p/pysendfile/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (python.org/~guido)
Hm. Why not just use "import socket" and then use "socket.AF_<whatever>"?
That's what I usually do as well (because explicit is better than implicit) but from my understanding when it comes to constants it is generally not considered a bad practice to import them directly into the module namespace. I guess my specific case is bit different though. I have all these constants defined in a _linux.py submodule which I import from __init__.py in order to expose them publicly. And this is how I do that: # Linux >= 2.6.36 if _psplatform.HAS_PRLIMIT: from psutil._pslinux import (RLIM_INFINITY, RLIMIT_AS, RLIMIT_CORE, RLIMIT_CPU, RLIMIT_DATA, RLIMIT_FSIZE, RLIMIT_LOCKS, RLIMIT_MEMLOCK, RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_RSS, RLIMIT_STACK) if hasattr(_psplatform, "RLIMIT_MSGQUEUE"): RLIMIT_MSGQUEUE = _psplatform.RLIMIT_MSGQUEUE if hasattr(_psplatform, "RLIMIT_NICE"): RLIMIT_NICE = _psplatform.RLIMIT_NICE if hasattr(_psplatform, "RLIMIT_RTPRIO"): RLIMIT_RTPRIO = _psplatform.RLIMIT_RTPRIO if hasattr(_psplatform, "RLIMIT_RTTIME"): RLIMIT_RTTIME = _psplatform.RLIMIT_RTTIME if hasattr(_psplatform, "RLIMIT_SIGPENDING"): RLIMIT_SIGPENDING = _psplatform.RLIMIT_SIGPENDING In *this specific case* a "from _psplatform import RLIM*" would have solved my problem nicely. On one hand this might look like encouraging wildcard import usage, but I think it's the opposite. Sometimes people use "from foo import *" just because "from foo import bar*" is not available. --- Giampaolo https://code.google.com/p/pyftpdlib/ https://code.google.com/p/psutil/ https://code.google.com/p/pysendfile/ On Thu, Oct 3, 2013 at 7:16 PM, Guido van Rossum <guido@python.org> wrote:
Hm. Why not just use "import socket" and then use "socket.AF_<whatever>"?
On Thu, Oct 3, 2013 at 10:09 AM, Giampaolo Rodola' <g.rodola@gmail.com>wrote:
I suppose this has already been proposed in past but couldn't find any online reference so here goes. When it comes to module constant imports I usually like being explicit it's OK with me as long as I have to do:
from resource import (RLIMIT_CORE, RLIMIT_CPU, RLIMIT_FSIZE)
Nevertheless in case the existence of certain constants depends on the platform in use I end up doing:
if hasattr(resource, "RLIMIT_MSGQUEUE"): # linux only .... import resource.RLIMIT_MSGQUEUE .... if hasattr(resource, "RLIMIT_NICE"): # linux only .... import resource.RLIMIT_NICE ....
...or worse, if for simplicity I'm willing to simply import all RLIMIT_* constants I'll have to do this:
import resource import sys for name in dir(resource): .... if name.startswith('RLIMIT_'): .... setattr(sys.modules[__name__], name, getattr(resource, name))
...or just give up and use:
from resource import *
...which of course will pollute the namespace with unnecessary stuff. So why not just allow "from resource import RLIMIT_*" syntax? Another interesting variation might be:
from socket import AF_*, SOCK_* AF_INET, AF_INET6, SOCK_STREAM, SOCK_DGRAM (2, 10, 1, 2)
On the other hand mixing "*" and "common" imports would be forbidden:
from socket import AF_*, socket, File "<stdin>", line 1 from socket import AF_*, socket ^ SyntaxError: invalid syntax;
Thoughts?
--- Giampaolo https://code.google.com/p/pyftpdlib/ https://code.google.com/p/psutil/ https://code.google.com/p/pysendfile/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (python.org/~guido)
Hm. It seems a pretty small use case for what would be a major implementation challenge -- I'm sure there would be lots of issues implementing this cleanly given all the special casing for import *, and the special handling of importlib during bootstrap. On Thu, Oct 3, 2013 at 11:43 AM, Giampaolo Rodola' <g.rodola@gmail.com>wrote:
Hm. Why not just use "import socket" and then use "socket.AF_<whatever>"?
That's what I usually do as well (because explicit is better than implicit) but from my understanding when it comes to constants it is generally not considered a bad practice to import them directly into the module namespace. I guess my specific case is bit different though. I have all these constants defined in a _linux.py submodule which I import from __init__.py in order to expose them publicly. And this is how I do that:
# Linux >= 2.6.36 if _psplatform.HAS_PRLIMIT: from psutil._pslinux import (RLIM_INFINITY, RLIMIT_AS, RLIMIT_CORE, RLIMIT_CPU, RLIMIT_DATA, RLIMIT_FSIZE, RLIMIT_LOCKS, RLIMIT_MEMLOCK, RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_RSS, RLIMIT_STACK) if hasattr(_psplatform, "RLIMIT_MSGQUEUE"): RLIMIT_MSGQUEUE = _psplatform.RLIMIT_MSGQUEUE if hasattr(_psplatform, "RLIMIT_NICE"): RLIMIT_NICE = _psplatform.RLIMIT_NICE if hasattr(_psplatform, "RLIMIT_RTPRIO"): RLIMIT_RTPRIO = _psplatform.RLIMIT_RTPRIO if hasattr(_psplatform, "RLIMIT_RTTIME"): RLIMIT_RTTIME = _psplatform.RLIMIT_RTTIME if hasattr(_psplatform, "RLIMIT_SIGPENDING"): RLIMIT_SIGPENDING = _psplatform.RLIMIT_SIGPENDING
In *this specific case* a "from _psplatform import RLIM*" would have solved my problem nicely. On one hand this might look like encouraging wildcard import usage, but I think it's the opposite. Sometimes people use "from foo import *" just because "from foo import bar*" is not available.
--- Giampaolo https://code.google.com/p/pyftpdlib/ https://code.google.com/p/psutil/ https://code.google.com/p/pysendfile/
On Thu, Oct 3, 2013 at 7:16 PM, Guido van Rossum <guido@python.org> wrote:
Hm. Why not just use "import socket" and then use "socket.AF_<whatever>"?
On Thu, Oct 3, 2013 at 10:09 AM, Giampaolo Rodola' <g.rodola@gmail.com>wrote:
I suppose this has already been proposed in past but couldn't find any online reference so here goes. When it comes to module constant imports I usually like being explicit it's OK with me as long as I have to do:
from resource import (RLIMIT_CORE, RLIMIT_CPU, RLIMIT_FSIZE)
Nevertheless in case the existence of certain constants depends on the platform in use I end up doing:
if hasattr(resource, "RLIMIT_MSGQUEUE"): # linux only .... import resource.RLIMIT_MSGQUEUE .... if hasattr(resource, "RLIMIT_NICE"): # linux only .... import resource.RLIMIT_NICE ....
...or worse, if for simplicity I'm willing to simply import all RLIMIT_* constants I'll have to do this:
import resource import sys for name in dir(resource): .... if name.startswith('RLIMIT_'): .... setattr(sys.modules[__name__], name, getattr(resource, name))
...or just give up and use:
from resource import *
...which of course will pollute the namespace with unnecessary stuff. So why not just allow "from resource import RLIMIT_*" syntax? Another interesting variation might be:
from socket import AF_*, SOCK_* AF_INET, AF_INET6, SOCK_STREAM, SOCK_DGRAM (2, 10, 1, 2)
On the other hand mixing "*" and "common" imports would be forbidden:
from socket import AF_*, socket, File "<stdin>", line 1 from socket import AF_*, socket ^ SyntaxError: invalid syntax;
Thoughts?
--- Giampaolo https://code.google.com/p/pyftpdlib/ https://code.google.com/p/psutil/ https://code.google.com/p/pysendfile/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (python.org/~guido)
-- --Guido van Rossum (python.org/~guido)
On Thu, Oct 3, 2013 at 9:00 PM, Guido van Rossum <guido@python.org> wrote:
Hm. It seems a pretty small use case for what would be a major implementation challenge -- I'm sure there would be lots of issues implementing this cleanly given all the special casing for import *, and the special handling of importlib during bootstrap.
Fair enough. --- Giampaolo https://code.google.com/p/pyftpdlib/ https://code.google.com/p/psutil/ https://code.google.com/p/pysendfile/
On 3 October 2013 19:43, Giampaolo Rodola' <g.rodola@gmail.com> wrote:
Hm. Why not just use "import socket" and then use "socket.AF_<whatever>"?
That's what I usually do as well (because explicit is better than implicit) but from my understanding when it comes to constants it is generally not considered a bad practice to import them directly into the module namespace. I guess my specific case is bit different though. I have all these constants defined in a _linux.py submodule which I import from __init__.py in order to expose them publicly. And this is how I do that:
# Linux >= 2.6.36 if _psplatform.HAS_PRLIMIT: from psutil._pslinux import (RLIM_INFINITY, RLIMIT_AS, RLIMIT_CORE, RLIMIT_CPU, RLIMIT_DATA, RLIMIT_FSIZE, RLIMIT_LOCKS, RLIMIT_MEMLOCK, ...
In *this specific case* a "from _psplatform import RLIM*" would have solved my problem nicely.
Or we change the module such that we can do from psutil._pslinux import RLIMIT and then use RLIMIT.CORE, RLIMIT.CPU, RLIMIT.LOCKS, etc.
On 4 Oct 2013 06:01, "Joshua Landau" <joshua@landau.ws> wrote:
On 3 October 2013 19:43, Giampaolo Rodola' <g.rodola@gmail.com> wrote:
Hm. Why not just use "import socket" and then use
"socket.AF_<whatever>"?
That's what I usually do as well (because explicit is better than
implicit)
but from my understanding when it comes to constants it is generally not considered a bad practice to import them directly into the module namespace. I guess my specific case is bit different though. I have all these constants defined in a _linux.py submodule which I import from __init__.py in order to expose them publicly. And this is how I do that:
# Linux >= 2.6.36 if _psplatform.HAS_PRLIMIT: from psutil._pslinux import (RLIM_INFINITY, RLIMIT_AS, RLIMIT_CORE, RLIMIT_CPU, RLIMIT_DATA, RLIMIT_FSIZE, RLIMIT_LOCKS, RLIMIT_MEMLOCK, ...
In *this specific case* a "from _psplatform import RLIM*" would have solved my problem nicely.
Or we change the module such that we can do
from psutil._pslinux import RLIMIT
and then use RLIMIT.CORE, RLIMIT.CPU, RLIMIT.LOCKS, etc.
Another Enum candidate, perhaps? Cheers, Nick.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas
Well, that looks painful! I agree with Joshua: If you are doing something like that, namespaces work best. If you're really that desperate, why not something like this: globals().update({name: getattr(_psplatform, name) for name in dir(_psplatform) if name.startswith('RLIMIT')}) On Thu, Oct 3, 2013 at 1:43 PM, Giampaolo Rodola' <g.rodola@gmail.com>wrote:
Hm. Why not just use "import socket" and then use "socket.AF_<whatever>"?
That's what I usually do as well (because explicit is better than implicit) but from my understanding when it comes to constants it is generally not considered a bad practice to import them directly into the module namespace. I guess my specific case is bit different though. I have all these constants defined in a _linux.py submodule which I import from __init__.py in order to expose them publicly. And this is how I do that:
# Linux >= 2.6.36 if _psplatform.HAS_PRLIMIT: from psutil._pslinux import (RLIM_INFINITY, RLIMIT_AS, RLIMIT_CORE, RLIMIT_CPU, RLIMIT_DATA, RLIMIT_FSIZE, RLIMIT_LOCKS, RLIMIT_MEMLOCK, RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_RSS, RLIMIT_STACK) if hasattr(_psplatform, "RLIMIT_MSGQUEUE"): RLIMIT_MSGQUEUE = _psplatform.RLIMIT_MSGQUEUE if hasattr(_psplatform, "RLIMIT_NICE"): RLIMIT_NICE = _psplatform.RLIMIT_NICE if hasattr(_psplatform, "RLIMIT_RTPRIO"): RLIMIT_RTPRIO = _psplatform.RLIMIT_RTPRIO if hasattr(_psplatform, "RLIMIT_RTTIME"): RLIMIT_RTTIME = _psplatform.RLIMIT_RTTIME if hasattr(_psplatform, "RLIMIT_SIGPENDING"): RLIMIT_SIGPENDING = _psplatform.RLIMIT_SIGPENDING
In *this specific case* a "from _psplatform import RLIM*" would have solved my problem nicely. On one hand this might look like encouraging wildcard import usage, but I think it's the opposite. Sometimes people use "from foo import *" just because "from foo import bar*" is not available.
--- Giampaolo https://code.google.com/p/pyftpdlib/ https://code.google.com/p/psutil/ https://code.google.com/p/pysendfile/
On Thu, Oct 3, 2013 at 7:16 PM, Guido van Rossum <guido@python.org> wrote:
Hm. Why not just use "import socket" and then use "socket.AF_<whatever>"?
On Thu, Oct 3, 2013 at 10:09 AM, Giampaolo Rodola' <g.rodola@gmail.com>wrote:
I suppose this has already been proposed in past but couldn't find any online reference so here goes. When it comes to module constant imports I usually like being explicit it's OK with me as long as I have to do:
from resource import (RLIMIT_CORE, RLIMIT_CPU, RLIMIT_FSIZE)
Nevertheless in case the existence of certain constants depends on the platform in use I end up doing:
if hasattr(resource, "RLIMIT_MSGQUEUE"): # linux only .... import resource.RLIMIT_MSGQUEUE .... if hasattr(resource, "RLIMIT_NICE"): # linux only .... import resource.RLIMIT_NICE ....
...or worse, if for simplicity I'm willing to simply import all RLIMIT_* constants I'll have to do this:
import resource import sys for name in dir(resource): .... if name.startswith('RLIMIT_'): .... setattr(sys.modules[__name__], name, getattr(resource, name))
...or just give up and use:
from resource import *
...which of course will pollute the namespace with unnecessary stuff. So why not just allow "from resource import RLIMIT_*" syntax? Another interesting variation might be:
from socket import AF_*, SOCK_* AF_INET, AF_INET6, SOCK_STREAM, SOCK_DGRAM (2, 10, 1, 2)
On the other hand mixing "*" and "common" imports would be forbidden:
from socket import AF_*, socket, File "<stdin>", line 1 from socket import AF_*, socket ^ SyntaxError: invalid syntax;
Thoughts?
--- Giampaolo https://code.google.com/p/pyftpdlib/ https://code.google.com/p/psutil/ https://code.google.com/p/pysendfile/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (python.org/~guido)
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas
-- Ryan
On 03/10/2013 18:09, Giampaolo Rodola' wrote:
I suppose this has already been proposed in past but couldn't find any online reference so here goes. When it comes to module constant imports I usually like being explicit it's OK with me as long as I have to do:
from resource import (RLIMIT_CORE, RLIMIT_CPU, RLIMIT_FSIZE)
Nevertheless in case the existence of certain constants depends on the platform in use I end up doing:
if hasattr(resource, "RLIMIT_MSGQUEUE"): # linux only .... import resource.RLIMIT_MSGQUEUE .... if hasattr(resource, "RLIMIT_NICE"): # linux only .... import resource.RLIMIT_NICE ....
...or worse, if for simplicity I'm willing to simply import all RLIMIT_* constants I'll have to do this:
import resource import sys for name in dir(resource): .... if name.startswith('RLIMIT_'): .... setattr(sys.modules[__name__], name, getattr(resource, name))
...or just give up and use:
from resource import *
...which of course will pollute the namespace with unnecessary stuff. So why not just allow "from resource import RLIMIT_*" syntax? Another interesting variation might be:
from socket import AF_*, SOCK_* AF_INET, AF_INET6, SOCK_STREAM, SOCK_DGRAM (2, 10, 1, 2)
On the other hand mixing "*" and "common" imports would be forbidden:
from socket import AF_*, socket, File "<stdin>", line 1 from socket import AF_*, socket ^ SyntaxError: invalid syntax;
Thoughts?
If you're importing RLIMIT_MSGQUEUE, then presumably you're using it somewhere(!), but if it's platform-specific, you'll still need to check which platform the code is running on anyway before trying to use it...
03.10.13 20:09, Giampaolo Rodola' написав(ла):
Another interesting variation might be:
from socket import AF_*, SOCK_* AF_INET, AF_INET6, SOCK_STREAM, SOCK_DGRAM (2, 10, 1, 2)
from socket import AddressFamily, SocketType globals().update(AddressFamily.__members__) globals().update(SocketType.__members__) AF_INET, AF_INET6, SOCK_STREAM, SOCK_DGRAM (<AddressFamily.AF_INET: 2>, <AddressFamily.AF_INET6: 10>, <SocketType.SOCK_STREAM: 1>, <SocketType.SOCK_DGRAM: 2>)
participants (7)
-
Giampaolo Rodola'
-
Guido van Rossum
-
Joshua Landau
-
MRAB
-
Nick Coghlan
-
Ryan Gonzalez
-
Serhiy Storchaka