Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm making a fairly simple weather module for myself and I have run into a major issue, I can't get a value from my module.
First, let me show you what I have so I can point out my issue.
PyWeather.py:
import urllib2
import json
import time
class get:
def __init__(self, location):
self.location = location
def status(self):
input = self.location
fixedinput = input.replace(" ","%20")
response = urllib2.urlopen('http://api.openweathermap.org/data/2.5/weather?q=' + fixedinput)
data = json.load(response)
weather = data['weather'][0]['main']
return weather
Main.py:
import PyWeather
location = 'Lexington, SC'
current = PyWeather.get(location).status
print current
I'm a little more than a beginner in Python, but I taught myself, so I don't understand quite a bit.
My issue lies in the output:
<bound method get.status of <PyWeather.get instance at 0x01925940>>
How do I get an output such as 'Clouds' (Which is the current condition)
You are not calling the function. You are just creating an alias to the function using the current variable name. Try
current = PyWeather.get(location).status() # Notice the ()
current = PyWeather.get(location).status()
should fix the issue
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
Im doing a program that generates a XML file using the element tree library, in my xml file i need a tag named Station with two subtags called Property and ProductionLine, so i made this class for it
from .Property import Property
from .ProductionLine import ProductionLine
class Station:
productionLine: ProductionLine
property: Property
def __init__(self, property: Property, productionLine: ProductionLine) -> None:
self.property = property
self.productionLine: productionLine
but, when i create a object from this class, aparently it only has the "property" attribute
property = Property(properties)
productionLineElement = ProductionLine(production_line)
station = Station(property, productionLineElement)
i checked it with print(station.__dict__) and this is what returns
{'property': <xmlgenerator.objects.Property.Property object at 0x7fb00dc5a910>}
Any idea of why only the "property" attribute its being shown? Any help would be apreciated
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I'm making a game and adding enemies to them, but for some reason, when I try to use the .get_rect() method, I get an error message saying "invalid rect assignment". I've tried many ways, if you can find a way to fix it, please let me know. Also, please, do feel free to criticise my work, as long as you provide me with solutions to fix that. I really want to improve.
python
class enemy():
def __init__(self, x, y, eid):
self.x = x
self.y = y
self.eid = eid
self.enemysurf = pygame.image.load("graphics/enemy.png")
self.enemyrect = self.enemysurf.get_rect(center=(self.x, self.y) # I get the error message here
screen.blit(self.enemysurf, self.enemyrect)
The issue is just a missing close parenthesis. This code should work:
self.enemyrect = self.enemysurf.get_rect(center=(self.x, self.y)
self.enemyrect = self.enemysurf.get_rect(center=(self.x, self.y))
Also, calling .convert() on pygame.image.load("graphics/enemy.png") makes the code faster. Lastly, if I remember your initial code correctly, you should always put the 'import' statements at the beginning of your code. More on the python style guide
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to call a function (my_function), where clearly in my code I am calling it but if the function is not executing.
Someone knows what this is??
Here is my code:
import pandas as pd
my_variable = "1"
if my_variable == "1":
my_function()
global my_function
def my_function():
def tryloc(df, col, idx, default=None):
try:
return df.iloc[col, idx]
except IndexError:
return default
print("hello")
edit = pd.read_csv("priv/productos.csv")
product = tryloc(edit, 1, 0)
print(product)
and the second problem is that it says that this function does not exist when I am declaring it globally
Thanks if you answer!
Despite popular belief, "it's not working" isn't adequate information to diagnose a problem. That said, you are calling the function before it is even declare. You must declare a function before it can be called, always.Try moving
myFunction();
after where you defined it. But, there is a lot more going on here that's wrong and the question doesn't explain what the desired outcome is so I'm not sure how to help. I recommend going back and checking out some python scope documents.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I'm having some trouble with Python mock() and I'm not familiar enough to figure out what's going on with it.
I have an abstract async task class that looks something like:
class AsyncTask(object):
#classmethod
def enqueue(cls):
....
task_ent = cls.createAsyncTask(body, delayed=will_delay)
....
I'd like to patch the createAsyncTask method for a specific instance of this class.
The code I wrote looks like:
#patch.object(CustomAsyncTaskClass, "createAsyncTask")
def test_my_test(self, mock_create_task):
....
mock_create_task.return_value = "12"
fn() # calls CustomAsyncTaskClass.enqueue(...)
....
When I print out task_ent in enqueue, I get <MagicMock name='createAsyncTask()' id='140578431952144'>
When I print out cls.createAsyncTask in enqueue, I get <MagicMock name='createAsyncTask' id='140578609336400'>
What am I doing wrong? Why won't createAsyncTask return 12?
Try the following:
#patch("package_name.module_name.createAsyncTask")
def test_my_test(self, mock_create_task):
....
mock_create_task.return_value = "12"
fn() # calls CustomAsyncTaskClass.enqueue(...)
....
where module_name is the name of the module which contains the class AsyncTask.
In general, this is the guideline https://docs.python.org/3/library/unittest.mock.html#where-to-patch
I know that this question is old but I just had the same problem and fixed it now.
If you patch multiple functions it is very important to keep the order in mind. It has to be reversed from the patches.
#patch("package_name.function1")
#patch("package_name.function2")
def test_method(
mocked_function2: MagicMock,
mocked_function1: MagicMock
)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Why cant I use randrange in this function?
import random
print (random.randrange(0,4))
def random(dice):
dice_one = random.randrange(1,3)
return (dice_one)
print (random(4))
This is the error
line 6, in random
dice_one = random.randrange(1,3)
AttributeError: 'function' object has no attribute 'randrange'
Thanks for any help
Python thinks you're trying to call a method of your own random function since you named it the same as the random module. Change your function name to something else and it should be fine.
def chance(dice):
dice_one = random.randrange(1,3)
return dice_one
print (chance(4))
Also, not sure what you're trying to do with the 'dice' argument. If this is meant to be the number of sides on the die, try:
def chance(dice):
dice_one = random.randrange(1,dice)
return dice_one
funny ,actually I am not able to understand which kind of output do you expect but from whatever I have understood the solution of program is this !
import random
print random.randrange(0,4)
def random_number(dice):
dice_one = random.randrange(1,3)
return dice_one
print random_number(4)