Comments (11)
I agree that restricting variables to be only visible within the p5 module is much more pythonic than the current approach. Although this change will break backward compatibility, but given that we are still in alpha versions, this probably is acceptable. @tushar5526 Feel free to open a PR on this once you get a chance!
from p5.
I will do this after skia, to avoid hidden bugs due to this.
from p5.
(just sent you a message on reddit about this)
from p5.
Is there a good way to wrap all the global variables into a namespace?
If I understand this correctly: you could just do: import p5
instead of from p5 import *
and then prefix all your commands with p5.
. You would then use p5.line(...)
, p5.circle(...)
etc, for instance.
Alternatively, I wonder if you could generate mappings for all the global variables.
What do you mean? Can't you just use:
>>> import p5
>>> dir(p5)
from p5.
for functions, yes. But other ambiguous global variables, no. For instance... frame_rate
. It might work well to add these globals to a separate class. poorly named example:
import p5
# to use framerate
global_setup = GlobalSetup()
global_setup.frame_rate
By doing this, your IDE wouldn't give you hell, and it would point directly to a place in code with doc strings etc...
having this type of namespace will also make other features down the road much easier... ie a frame_buffer to export frames to images
from p5.
using this test branch this file could be written this way.
Is that something you'd considered, the changes are somewhat huge...
❯ diff namespace.py profiling/arcs.py
1,2c1
< import p5
<
---
> from p5 import *
5c4
< p5.size(720, 400)
---
> size(720, 400)
10,15c9,14
< while x < 2 * p5.PI:
< p5.arc((50.0 + offset, 50.0), 50.0, 50.0, x, 2 * p5.PI)
< p5.arc((50.0 + offset, 100), 50, 50, x, 2 * p5.PI, mode='OPEN')
< p5.arc((50.0 + offset, 150), 50, 50, x, 2 * p5.PI, mode='CHORD')
< p5.arc((50.0 + offset, 200), 50, 50, x, 2 * p5.PI, mode='PIE')
< x += p5.QUARTER_PI
---
> while x < 2 * PI:
> arc((50.0 + offset, 50.0), 50.0, 50.0, x, 2 * PI)
> arc((50.0 + offset, 100), 50, 50, x, 2 * PI, mode='OPEN')
> arc((50.0 + offset, 150), 50, 50, x, 2 * PI, mode='CHORD')
> arc((50.0 + offset, 200), 50, 50, x, 2 * PI, mode='PIE')
> x += QUARTER_PI
19c18
< p5.run()
---
> run()
from p5.
I encountered also some problems with the lack of namespace for variables
Generally speaking, instead of using
from p5 import *
I ask my student to use
import p5
Yes, all function have to be prefixed this way
p5.circle(10,10,10)
...
But this is what I want
What is the problem for me is I expected the variables to act the same way.
But P5.frame_count is inexistant. I have to use frame_count
Not very problematic, except for editors work. But more problematic is the use of global variables for width and height. This is clearly polluting the global namespace.
Any chance that variables in p5 will become soon part of p5 namespace ?
from p5.
No news about this issue. I have to say that there is also width and height that are imported from p5 and "pollute" the global namespace. It's inadequate, because width and height can be used by programmers !
from p5.
The possible solution to overcome this issue will then expose global variables as p5.width
for width
while importing as follows from p5 import *
This will not align properly with Processing's API, but we can implement it for p5py if the community needs it.
from p5.
Thank you for your response. It is not aligned with processing API, but it is correct according to the pythonic way of creating modules and not polluting global namespace
Those wanting to do so just use
from p5 import *
indeed ?
from p5.
I tried fiddling with some code and it's possible to remove the variables from global
namespace. We could simply use p5.widthX
or the other way if we still want to use global variables and discard the use of builtins, is to inject the variables into the global namespace by appending them to __globals__
(but this hack might haunt many later).
The better approach seems to go with the convention of scoping variables to p5 (even though it's different from what we have in p5.js, but p5py was developed with the aim that we could have the benefits of processing with other python libraries)
from p5.
Related Issues (20)
- Unexpected behavior of size() in setup() HOT 4
- p5/ Skia in Pycharm rendering just a black screen HOT 15
- Weird jittery behavior when `background()` is not called in `draw()` while using the skia renderer. HOT 12
- Exclude building test files into distribution builds HOT 2
- extend CI tests for windows and mac-os platform HOT 2
- Too many open files error when loading many images HOT 3
- Migrate to poetry HOT 4
- save() throws an error HOT 2
- Errors using "CORNERS" mode HOT 5
- Error installing p5 package: Failed building wheel for PyOpenGL-accelerate HOT 13
- Inconsistency between mouse_is_pressed boolean and mouse_clicked function HOT 1
- Improve installation instructions to account for unuspported Python versions HOT 6
- Setup pre-commit HOT 3
- Optimize skia rendering loop
- KeyError: 'SQAURE' when using stroke_cap(SQUARE) HOT 5
- Fix Code Formatting to Resolve Flake8 Errors in Pre-commit Hook HOT 4
- ModuleNotFoundError: No module named 'npy_tempita' when installing P5. HOT 3
- Not compatible with Python >=3.11 HOT 6
- Unable to install p5 and ml5 in python HOT 2
- pip cant install p5 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from p5.