4import requests
5
6@app.on_message(filters.command("stack", prefix) & filters.me)
7async def stack(app, msg): 8 await msg.edit("Processing...")
9 if len(msg.command) < 2:
10 await msg.edit(f"`{prefix}stack` **query**")
4import requests
5
6@app.on_message(filters.command("repo", prefix) & filters.me)
7async def search_repo(app, msg): 8 url = "https://api.github.com/search/repositories?q="
9 if len(msg.command) < 2:
10 await msg.edit(f"`{prefix}search-repo` **query**")
33
34# create query command for neko2
35@app.on_message(filters.command("neko2", prefix) & filters.me)
36async def neko2(app, msg):37 url = "https://nekos.life/api/v2/img/"
38 if len(msg.command) < 2:
39 await msg.edit(f"`{prefix}neko2` **category**")
55
56# list of categories from data
57@app.on_message(filters.command("neko2list", prefix) & filters.me)
58async def neko2list(app, msg):59 # parse data to string and make list of categories
60 categories = "\n-".join(data)
61 # usage of categories
43 if r.status_code == 404:
44 await msg.edit("Category not found")
45 return
46 data = r.json()47 url = data["url"]
48 await msg.edit(f"**Neko for {category}**")
49 # fix gif image
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
Another usual suspect of this is when you use the same parameter name inside a function as the global variable you are using. For example:
def run_app(app):
# This `app` shadows the global app...
app.run()
if __name__ == '__main__':
app = MyApp() # This is a global variable!
run_app(app)
To avoid this re-defining of a global, consider not defining app
as a global, but inside a main()
function instead:
def run_app(app):
# There is no longer a global `app` variable.
app.run()
def main():
app = MyApp()
run_app(app)
if __name__ == '__main__':
main()