pygame - importing GL - very bad...

S

someone

See this code (understand why I commented out first line):

# from OpenGL.GL import *
from OpenGL.GL import glEnable, GL_DEPTH_TEST, \
glShadeModel, GL_SMOOTH, glClearColor, \
GL_CULL_FACE, GL_BLEND, glBlendFunc, \
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, \
glClear, GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, \
glLoadIdentity, glTranslate, glRotate, \
glMultMatrixf, glPushMatrix, glCallList, \
glPopMatrix, glDisable, GL_LIGHTING

The reason why I commented out the first line is that I use "pylint" and
it reports: "[W] Redefining built-in 'format'" for this line.

From: http://www.logilab.org/card/pylintfeatures tell:
W0621: Redefining name %r from outer scope (line %s) Used when a
variable's name hide a name defined in the outer scope.

I don't like to redefine already defined names so therefore I had to
outcomment first line and then keep on adding stuff until I could run my
program... But this SUCKS! I can see that pygame hasn't been updated for
a long while - not many users use it? I'm not very happy about this...

Any good / clever solution to this problem, so I avoid this nasty crappy
work-around?

Any ideas / suggestions ?
Thanks.
 
C

Chris Angelico

See this code (understand why I commented out first line):

# from OpenGL.GL import *
from OpenGL.GL import glEnable, GL_DEPTH_TEST, \
glShadeModel, GL_SMOOTH, glClearColor, \
GL_CULL_FACE, GL_BLEND, glBlendFunc, \
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, \
glClear, GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, \
glLoadIdentity, glTranslate, glRotate, \
glMultMatrixf, glPushMatrix, glCallList, \
glPopMatrix, glDisable, GL_LIGHTING

Any good / clever solution to this problem, so I avoid this nasty crappy
work-around?

You could simply

import OpenGL.GL as GL

and then use all those names as GL.glPopMatrix, GL.GL_LIGHTING, etc. I
don't know if that's better or worse.

ChrisA
 
S

Steven D'Aprano

See this code (understand why I commented out first line):

# from OpenGL.GL import * [...]
The reason why I commented out the first line is that I use "pylint" and
it reports: "[W] Redefining built-in 'format'" for this line.

From: http://www.logilab.org/card/pylintfeatures tell: W0621: Redefining
name %r from outer scope (line %s) Used when a variable's name hide a
name defined in the outer scope.

I don't like to redefine already defined names so therefore I had to
outcomment first line and then keep on adding stuff until I could run my
program... But this SUCKS! I can see that pygame hasn't been updated for
a long while - not many users use it? I'm not very happy about this...

from pygame import *
del format


pylint may still complain, but you can ignore it. By deleting the name
"format", that will unshadow the builtin format.
 
P

Peter Otten

someone said:
See this code (understand why I commented out first line):

# from OpenGL.GL import *
from OpenGL.GL import glEnable, GL_DEPTH_TEST, \
glShadeModel, GL_SMOOTH, glClearColor, \
GL_CULL_FACE, GL_BLEND, glBlendFunc, \
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, \
glClear, GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, \
glLoadIdentity, glTranslate, glRotate, \
glMultMatrixf, glPushMatrix, glCallList, \
glPopMatrix, glDisable, GL_LIGHTING

The reason why I commented out the first line is that I use "pylint" and
it reports: "[W] Redefining built-in 'format'" for this line.

From: http://www.logilab.org/card/pylintfeatures tell:
W0621: Redefining name %r from outer scope (line %s) Used when a
variable's name hide a name defined in the outer scope.

I don't like to redefine already defined names so therefore I had to
outcomment first line and then keep on adding stuff until I could run my
program... But this SUCKS! I can see that pygame hasn't been updated for
a long while - not many users use it? I'm not very happy about this...

Any good / clever solution to this problem, so I avoid this nasty crappy
work-around?

Any ideas / suggestions ?
Thanks.

It turns out pylint is lying. The situation is equivalent to

$ cat module.py
for format in [42]:
pass
del format

$ cat main.py
original_format = format
from module import *
assert format is original_format
$ python main.py

The assert doesn't trigger, so format is not redefined. But pylint complains
anyway:

$ pylint main.py -rn
No config file found, using default configuration
************* Module main
W: 2: Redefining built-in 'format'
C: 1: Missing docstring
C: 1: Invalid name "original_format" (should match (([A-Z_][A-Z0-9_]*)|
(__.*__))$)
W: 2: Wildcard import module

If you can ignore the warning about the wildcard import you should be able
to ignore the "redefining built-in" warning, too. Personally I would avoid
putting magic comments like

from module import * # pylint: disable=W0622

$ pylint main.py -rn
No config file found, using default configuration
************* Module main
I: 2: Locally disabling W0622
C: 1: Missing docstring
C: 1: Invalid name "original_format" (should match (([A-Z_][A-Z0-9_]*)|
(__.*__))$)
W: 2: Wildcard import module

into the code.
 
S

someone

You could simply

import OpenGL.GL as GL

and then use all those names as GL.glPopMatrix, GL.GL_LIGHTING, etc. I
don't know if that's better or worse.

You're right - but I forgot to write that even though this maybe
should/is recommended many places then I've seen a lot of opengl code on
the internet and IMHO NOBODY does that and it'll be a lot slower to type
that in front of all the opengl commands...

So this solution is not something I like too... But I can see some other
people came up with good solutions, which I didn't knew about..

Thank you.
 
S

someone

See this code (understand why I commented out first line):

# from OpenGL.GL import * [...]
The reason why I commented out the first line is that I use "pylint" and
it reports: "[W] Redefining built-in 'format'" for this line.

From: http://www.logilab.org/card/pylintfeatures tell: W0621: Redefining
name %r from outer scope (line %s) Used when a variable's name hide a
name defined in the outer scope.

I don't like to redefine already defined names so therefore I had to
outcomment first line and then keep on adding stuff until I could run my
program... But this SUCKS! I can see that pygame hasn't been updated for
a long while - not many users use it? I'm not very happy about this...

from pygame import *
del format

Are you sure about this? Because I'm not (OTOH I'm maybe not as
experienced in python as some of you)... Ipython log:

--------
In [6]: test=format(43)

In [7]: type(test)
Out[7]: str

In [8]: from pygame import *

In [9]: test=format(43)

In [10]: type(test)
Out[10]: str

In [11]: del format
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-028e6ffb84a8> in <module>()
----> 1 del format

NameError: name 'format' is not defined
--------

What does this mean? Why does it say 'format" cannot be deleted after I
did the wildcard import ?
pylint may still complain, but you can ignore it. By deleting the name
"format", that will unshadow the builtin format.

Are you sure?
 
S

someone

someone wrote:
It turns out pylint is lying. The situation is equivalent to

$ cat module.py
for format in [42]:
pass
del format

$ cat main.py
original_format = format
from module import *
assert format is original_format
$ python main.py

The assert doesn't trigger, so format is not redefined. But pylint complains
anyway:

$ pylint main.py -rn
No config file found, using default configuration
************* Module main
W: 2: Redefining built-in 'format'
C: 1: Missing docstring
C: 1: Invalid name "original_format" (should match (([A-Z_][A-Z0-9_]*)|
(__.*__))$)
W: 2: Wildcard import module

If you can ignore the warning about the wildcard import you should be
able

In the case of opengl import, I'll ignore the wildcard import warning.
But in other cases I'll likely look a bit into it and see if I can avoid
it or at least change it to something like: "import OpenGL.GL as GL" etc.
to ignore the "redefining built-in" warning, too. Personally I would avoid
putting magic comments like

from module import * # pylint: disable=W0622

Oh, I just learned something new now... How come I cannot type "#pylint:
enable=W0622" in the line just below the import ?

Not so intuitively/logically for me...
$ pylint main.py -rn
No config file found, using default configuration
************* Module main
I: 2: Locally disabling W0622
C: 1: Missing docstring
C: 1: Invalid name "original_format" (should match (([A-Z_][A-Z0-9_]*)|
(__.*__))$)
W: 2: Wildcard import module

into the code.

Thank you very much...

Another thing is that I don't understand this warning:

Invalid name "original_format" (should match (([A-Z_][A-Z0-9_]*)|
(__.*__))$)

I get it everywhere... I don't understand how it wants me to label my
variables... Maybe I should disable this warning to get rid of it...
 
S

someone

It helps if you look in the right place:

pygame "Last Updated 2012-12-29": https://bitbucket.org/pygame/pygame/src
pygame2: http://code.google.com/p/pgreloaded/

Maybe... But if you look for stable releases here:

http://www.pygame.org/download.shtml

You'll find the top option: 1.9.1 Packages (August 6th 2009)


And then previous releases is just below 1.9.1:

pygame-1.9.0release.tar.gz ~ 1.4M - August 1, 2009
pygame-1.8.1release.tar.gz ~ 1.4M - July 30, 2008
pygame-1.8.0release.tar.gz ~ 1.4M - March 29, 2008
pygame-1.7.1release.tar.gz ~ 1.3M - August 16, 2005
1.7.0 ~ no source release was made.
pygame-1.6.2.tar.bz2 ~ 1140 kb -
pygame-1.6.tar.gz ~ 832 kb - October 23, 2003
pygame-1.5.tar.gz ~ 736 kb - May 30, 2002
pygame-1.4.tar.gz ~ 808 kb - Jan 30, 2002
pygame-1.3.tar.gz ~ 731 kb - Dec 19, 2001
pygame-1.2.tar.gz ~ 708 kb - Sep 4, 2001
pygame-1.1.tar.gz ~ 644 kb - Jun 23, 2001
pygame-1.0.tar.gz ~ 564 kb - Apr 5, 2001
pygame-0.9.tar.gz ~ 452 kb - Feb 13, 2001
pygame-0.5.tar.gz ~ 436 kb - Jan 6 14, 2001
pygame-0.4.tar.gz ~ 420 kb - Dec 14, 2000
pygame-0.3b.tar.gz ~ 367 kb - Nov 20, 2000
pygame-0.2b.tar.gz ~ 408 kb - Nov 3, 2000
pygame-0.1a.tar.gz ~ 300 kb - Oct 28, 2000


Back to year 2000...

Maybe they should get a grip on themselves and distribute a new stable
releases in year 2013 - then it would at least SEEM to look as the
project is not dead. But in any case, I'm happy with it - haven't
experienced any big issues with pygame yet, so don't take this as I
don't value what they do. Maybe they've made a great version back in
2009 and it's so good that there wasn't any need for a newer stable
version before 2013.

But it gives the impression that nothing happens, when so many years
pass on...

Anyway, thanks a lot to all...

(And sorry I accidentally replied privately to some of you - in
thunderbird I should hit the "followup"-button but maybe they've removed
it and instead I keep on hitting "reply" - very confusing that the first
button in thunderbird is reply instead of followup, which is what I
always prefer to use (so other people can see the answers).

Thanks you for pointing out that (at least) something did happen on
2012-12-29, when it looks a bit dead on the official homepage.
 
N

Nobody

In [11]: del format
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-028e6ffb84a8> in <module>()
----> 1 del format

NameError: name 'format' is not defined

You can't delete built-in names.

It has nothing to do with the wildcard import. The PyOpenGL modules delete
"format" from the module's variables as soon as they are finished with
it, so the set of names created by the wildcard import doesn't include
"format".
 
S

someone

In [11]: del format
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-028e6ffb84a8> in <module>()
----> 1 del format

NameError: name 'format' is not defined

You can't delete built-in names.

Ah, ok - and cannot overwrite it too, I guess... A shame that pylint
didn't knew about this.
It has nothing to do with the wildcard import. The PyOpenGL modules delete
"format" from the module's variables as soon as they are finished with
it, so the set of names created by the wildcard import doesn't include
"format".

Ok, sounds to me like I can safely ignore this pylint warning in any
case... Thanks!
 
S

Steven D'Aprano

See this code (understand why I commented out first line):

# from OpenGL.GL import * [...]
The reason why I commented out the first line is that I use "pylint"
and it reports: "[W] Redefining built-in 'format'" for this line.

From: http://www.logilab.org/card/pylintfeatures tell: W0621:
Redefining name %r from outer scope (line %s) Used when a variable's
name hide a name defined in the outer scope.

I don't like to redefine already defined names so therefore I had to
outcomment first line and then keep on adding stuff until I could
run my program... But this SUCKS! I can see that pygame hasn't been
updated for a long while - not many users use it? I'm not very happy
about this...

from pygame import *
del format

Are you sure about this? Because I'm not (OTOH I'm maybe not as
experienced in python as some of you)...

In the general case of deleting global names that shadow builtin names,
yes I am.

In the specific case of importing * from pygame, no. I trusted you that
pygame exports format. Unfortunately, it seems that you were fooled by an
invalid warning from pylint, so we were both mistaken.

Ipython log:

--------
In [6]: test=format(43)
In [7]: type(test)
Out[7]: str
In [8]: from pygame import *
In [9]: test=format(43)
In [10]: type(test)
Out[10]: str
In [11]: del format
------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-028e6ffb84a8> in <module>()
----> 1 del format

NameError: name 'format' is not defined

It means that there is no "format" in the current scope, which implies
that pygame no longer has a "format" which can be imported.

You don't need an import to shadow built-ins. See for example:

py> format
<built-in function format>
py> format = "NOBODY expects the Spanish Inquisition!"
py> format # this shadows the built-in "format"
'NOBODY expects the Spanish Inquisition!'
py> del format # get rid of the Spanish Inquisition
py> format
<built-in function format>
py> del format
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'format' is not defined


When a name is not discovered in the current scope, the builtin scope is
checked before Python gives up and reports a NameError. But del only
works on the current scope, to stop you from accidentally deleting the
wrong object.

Are you sure?

Since it turns out that pylint was actually wrong to complain, no format
was actually imported, then yes you can safely ignore it :)
 
A

alex23

You can't delete built-in names.

Actually, you can. If you ever need to shoot yourself in the foot in
this particular way, you can always do:

del __builtins__.format

Not saying you _should_, just that you _can_ :)
 
P

Peter Otten

someone said:
On 01/01/2013 01:56 PM, Peter Otten wrote:

Oh, I just learned something new now... How come I cannot type "#pylint:
enable=W0622" in the line just below the import ?

With what intended effect?
Another thing is that I don't understand this warning:

Invalid name "original_format" (should match (([A-Z_][A-Z0-9_]*)|
(__.*__))$)

I get it everywhere... I don't understand how it wants me to label my
variables... Maybe I should disable this warning to get rid of it...

pylint wants global names to be uppercase (what PEP 8 recommends for
constants) or "special" (two leading and two trailing underscores):

THATS_OK = 42
__thats_ok_too__ = object()
but_thats_not = "spam"
 
S

someone

Actually, you can. If you ever need to shoot yourself in the foot in
this particular way, you can always do:

del __builtins__.format

Not saying you _should_, just that you _can_ :)

Ok, good to know (not saying I would ever try it) :)
 
S

someone

It means that there is no "format" in the current scope, which implies
that pygame no longer has a "format" which can be imported.

You don't need an import to shadow built-ins. See for example:

py> format
<built-in function format>
py> format = "NOBODY expects the Spanish Inquisition!"
py> format # this shadows the built-in "format"
'NOBODY expects the Spanish Inquisition!'
py> del format # get rid of the Spanish Inquisition
py> format
<built-in function format>
py> del format
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'format' is not defined

Ok, thank you very much - that was/is very illustrative...
When a name is not discovered in the current scope, the builtin scope is
checked before Python gives up and reports a NameError. But del only
works on the current scope, to stop you from accidentally deleting the
wrong object.

Ok, I'll remember that in the future, thank you.
Since it turns out that pylint was actually wrong to complain, no format
was actually imported, then yes you can safely ignore it :)

Ok, I can see from your example that you're right. Nice to know the real
explanation, thank you very much. :)
 
S

someone

With what intended effect?

If I have a section with A LOT OF warnings and I don't want those in
that section to show up ? Isn't that valid enough?
Another thing is that I don't understand this warning:

Invalid name "original_format" (should match (([A-Z_][A-Z0-9_]*)|
(__.*__))$)

I get it everywhere... I don't understand how it wants me to label my
variables... Maybe I should disable this warning to get rid of it...

pylint wants global names to be uppercase (what PEP 8 recommends for
constants) or "special" (two leading and two trailing underscores):

THATS_OK = 42
__thats_ok_too__ = object()
but_thats_not = "spam"

OMG... I don't want to type those underscores everywhere... Anyway,
thank you very much for explaining the meaning of what it wants...
 
D

Dave Angel

With what intended effect?

If I have a section with A LOT OF warnings and I don't want those in
that section to show up ? Isn't that valid enough?
Another thing is that I don't understand this warning:

Invalid name "original_format" (should match (([A-Z_][A-Z0-9_]*)|
(__.*__))$)

I get it everywhere... I don't understand how it wants me to label my
variables... Maybe I should disable this warning to get rid of it...

pylint wants global names to be uppercase (what PEP 8 recommends for
constants) or "special" (two leading and two trailing underscores):

THATS_OK = 42
__thats_ok_too__ = object()
but_thats_not = "spam"

OMG... I don't want to type those underscores everywhere... Anyway,
thank you very much for explaining the meaning of what it wants...

Global const values should be ALL_CAPS, so it's obvious that nobody
intends to modify them. It's the non-const global attributes that
expect to be underscored.

You shouldn't have to use those underscores very often. After all,
there is seldom a need for a non-const global value, right? Don't think
of it as a pylint problem, but as a hint from pylint that perhaps you
should use fewer globals.
 
C

Chris Angelico

someone said:
Another thing is that I don't understand this warning:

Invalid name "original_format" (should match (([A-Z_][A-Z0-9_]*)|
(__.*__))$)

I get it everywhere... I don't understand how it wants me to label my
variables... Maybe I should disable this warning to get rid of it...

pylint wants global names to be uppercase (what PEP 8 recommends for
constants) or "special" (two leading and two trailing underscores):

THATS_OK = 42
__thats_ok_too__ = object()
but_thats_not = "spam"

Okay, I have to ask... why? Does it have an exception for names of classes?

I don't like linters that enforce too much style. Catch things that
might be mis-coded (like C's classic "if (x = 1)"), but don't complain
about my names. They're MY business.

ChrisA
 
I

Ian Kelly

Okay, I have to ask... why? Does it have an exception for names of classes?

Yes, and for module-level functions.
I don't like linters that enforce too much style. Catch things that
might be mis-coded (like C's classic "if (x = 1)"), but don't complain
about my names. They're MY business.

pylint is configurable though, so you can disable any warnings you
don't care about. My pylint macro has a fairly large number of -d
options in it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top