so I recently started looking into GUIs and I'm looking at a source code for the game wormy which can be found: here. I just have a few questions regarding this source code.
What does the sys module do in this code? I know that the sys module provides information about constants, functions and methods of the Python interpreter (taken from a website), but in this source code the only thing that the sys module is actually doing is being imported and then terminated when the game is closed? So I'm a bit confused about that.
In addition, I am wondering on how I can change the squares to look like circles instead? I tried changing the module functions (Rect and draw.rect) to draw.circle but an error keeps popping up. Any suggestions? Thanks!
When using pygame.draw.circle(), it will take different arguments than rect(). Look at the documentation here. Post your code and your error message for better help.
Related
I just started learning Python two weeks ago. I know this question is probably so basic. But please explain to me like I'm 5. I googled "vscode python import not working" etc but all the other cases seem way more complicated than mine.
I'm trying to follow a tutorial where they're introducing me to IDEs and showing that, if the a .py file is in the same directory as your current file, you can import it by typing "import file_name". Except it's not working for me:
It doesn't say there's an error or anything when I just run "import file_name" but says the function in the imported file doesn't exist in the current file if I try to run that.
I did not click on "add blah to PATH" thing when I installed. Could that be the problem?
Some google results say to make sure the debugger uses the correct working directory. Does it pertain to my situation and if it does, how do I do that? Some other results said to add the directory to PYTHONPATH but I didn't understand what that does, also it sounded like that sets up a path for the current file but doesn't establish that that's where I want to pull a file from in general when I type "import file_name" in any file I make from now on.
Please help, both in understanding the problem and fixing it. Thanks.
What I've done so far:
I thought maybe the file name was a keyword so I changed it from area to my_area but it still didn't work. I don't have any other ideas as to how to fix this.
In simple terms, the triangle function is imported as part of the my_area module, and so you'll need to get it from there as my_area.triangle:
import my_area
print(my_area.triangle(4, 5))
Alternatively, you can directly import triangle:
from my_area import triangle
print(triangle(4, 5))
Here's the python module tutorial for more info.
So I wanted to check out some implementations of standard libraries. I started with the os library with the code being here on github.
I took one method for example os.listdir() and I have absolutely no idea how it is implemented even after looking at the code ( pardon this noob ). I have following questions:
os.__all__ do not list this method but I think it is definitely a method as print(type(os.listdir)) listed <class 'builtin_function_or_method'> and I searched on google to find all the builtin functions which I found on this doc page and this is not one of them.
There is not such exclusive function named listdir defined in the module. In the code, from my limited understanding, the function is taken from globals() and put into a support_fd set. How this method is being called I do not understand.
I think the main problem I have is how that module is designed and I was not able to find any resources online to explain in simpler terms hence I am asking here for pointers.
EDIT: For those who are asking, I tried the following code in onlinegdb
import os
if "listdir" in os.__all__:
print("Yes")
print(os.listdir())
The result is only main.py, it should also print Yes, maybe the platform onlinegdb is the problem but it clearly shows the output of listdir as main.py.
After having discussion in the comments I see now that this is more of a online python version problem and not an issue with python or the module itself.
I've just started looking at the Vim jedi plugin, and it seems pretty impressive. One feature of some of the Java IDEs I've used is the ability to automatically add required imports. Can Jedi do that? For example, if I enter a line such as
arg1 = sys.argv[1]
and then invoke some Jedi command, is it possible for the plugin to automatically insert an import sys line at the top of the source file (if sys is not already being imported)?
I've looked through the Jedi help, and can't see anything like this - but it's possible I missed something. Alternatively, is there another Vim plugin that would do this? (It needs a certain level of understanding of Python syntax to get it right, which is why I looked to Jedi to be able to do it).
Currently Jedi doesn't do refactoringing. This includes import additions. There's an issue for the whole subject: https://github.com/davidhalter/jedi/issues/667.
It's not that easy to implement this command with good performance. However any help is appreciated. :)
FIY, I've defined a generic import feature that can be used on demand in lh-dev. I use it from my C&C++ suite, and from my snippet engine (mu-template).
So far I don't parse anything to add the missing import/include statements. This part would be complex as Dave said. Instead my snippets know which files need to be imported/included and import/include them if not already imported/included.
It's far from being perfect, but it's a start. mu-template provides a hook to do stuff at the start of the file after the snippet has been expanded, this is where I call lh-dev function. If other snippet engines provide similar hooks, you should be able to call lh#dev#import#add() from your snippets.
Here a proof of concept snippet for Python (I seldom program in Python, and don't have many snippets for it): https://github.com/LucHermitte/mu-template/blob/master/after/template/python/path-exists.template
I tried searching around but didn't seem to find an answer to my problem, so I'm sorry if I missed something and it actually has been answered before.
So basically I have main.py and another file called check.py (both in same directory)
In my main.py I have:
from check import checkfunction
I have a small function inside main.py that I MUST call inside check.py, but I can't seem to get this import working on my check.py:
from main import mainfunction
How can I get the mainfunction to work inside check.py?
Thanks!
You've got a design with a circular dependency which is usually a bad thing as your two python modules are tightly coupled.
Consider refactoring your code. But if you must stick with your design please see the following SO question for more info on how circular imports work in Python and the various gotchas to look out for.
Several options:
Move the common function to a module imported by both other modules.
Merge both modules into one.
Pass the function from main to the code that needs to call it.
Monkey patch the function into the check module after importing it.
Refactor the whole thing so that you don't have circular dependencies.
If you actually explained why you have this design, someone could possibly propose a better way.
I am trying to learn how Python reloads modules, but have hit a roadblock.
Let's say I have:
dir1\file1.py:
from dir2.file2 import ClassOne
myObject = ClassOne()
dir1\dir2\file2.py:
class ClassOne():
def reload_module():
reload(file2)
The reload call fails to find module "file2".
My question is, how do I do this properly, without having to keep everything in one file?
A related question: When the reload does work, will myObject use the new code?
thank you
def reload_module():
import file2
reload(file2)
However, this will not per se change the type of objects you've instantiated from classes held in the previous version of file2. The Python Cookbook 2nd edition has a recipe on how to accomplish such feats, and it's far too long and complex in both code and discussion to reproduce here (I believe you can read it on google book search, or failing that the original "raw" version [before all the enhancements we did to it], at least, should still be on the activestate cookbook online site).