I am currently having an issue, as i am relatively new to python , it might be a very easy solution for others.
I want to pass a parameter between both functions 'eg1' and 'eg2', there is a common number the user will input (example:10) then 'eg1' will add 1 to it and 'eg2' will take the final value of 'eg1' and add 1 more to it, (example: 10 will become 11 then 12)
It is troubling me because this keeps popping up:
Traceback (most recent call last):
File "example.py", line 69, in <module>
eg2(a)
File "example.py", line 63, in eg2
b = a.d
AttributeError: 'int' object has no attribute 'd'
I can't seem to find my mistake.
class Helper:pass
a = Helper()
def one(a):
d = a
d += 1
print d
def two(a):
b = a.d
b += 1
print b
print
print ("Please enter a number.")
a = int(input('>> ')
print
one(a)
print
two(a)
Reference for parameter passing:
Python definition function parameter passing
'Print' with nothing means to leave an empty line, for me
I messed up the title, fixed.
Since you are already using a class, you can pass the number you want to increment twice as an instance attribute, and call your increment functions on that attribute. This will avoid passing the updated value after calling one in the method two
Calling one and then two makes sure that two is working on the updated value after calling one.
class Helper:
# Pass num as parameter
def __init__(self, num):
self.num = num
# Increment num
def one(self):
self.num += 1
# Increment num
def two(self):
self.num += 1
h = Helper(10)
h.one()
print(h.num) # 11
h.two()
print(h.num) # 12
Based on your comments, here's one way to get the result. I am using python3:
class Helper:
d = None
cl = Helper()
def one(a):
cl.d = a
cl.d += 1
return cl.d
def two():
cl.d += 1
return cl.d
print ("Please enter a number.")
a = int(input('>> '))
print(one(a))
print(two())
Related
For example, I got a class named stack,
class stack: #Will be used for probable files!
def __init__(self):
self.data = []
def add(self, element):
self.data.append(element)
def number_of_elements(self):
return len(self.data)
def stackType(self):
if self.number_of_elements == 0:
return 0
elif self.number_of_elements == 1:
return 1
else:
return -1
I then do this:
foo = stack()
print foo.stackType()
I get -1 however I was expecting a return of 1
Why is it so and how can I handle with it?
That's because you did not call the call the method self.number_of_elements; you merely tested to see if it equalled 0 or 1.
Modify your code to actually call the method using this syntax: self.number_of_elements() [notice the use of () to call the method]:
def stackType(self) :
if self.number_of_elements() == 0 :
return 0
elif self.number_of_elements() == 1 :
return 1
else :
return -1
You could also have written it like this:
def stack_type(self):
n = self.number_of_elements()
return -1 if n > 1 else n
which would be an improvement because number_of_elements() will be called once only. In your code the method could be called twice. I renamed the function to be consistent with the Python method naming conventions set out in PEP8.
Because self.number_of_elements is not the same as self.number_of_elements()!
The former is a reference to the function, the latter is a call to the function actually calculating the length of your stack.
self.number_of_elements is a function, so its value is neither zero nor 1
I am trying to calculate the number of elements in a chemical equation. The debugger that I have created somehow doesn't have access to the globals within my program. Specifically, I am trying to access carrots but left is not being added to the stack. Any ideas?
Debug.py
class Debugger(object):
def __init__(self,objs):
assert type(objs)==list, 'Not a list of strings'
self.objs = objs
def __repr__(self):
return '<class Debugger>'
def show(self):
for o in self.objs:
print o,globals()[o] #EDIT
Chemical_Balancer.py
from Debug import Debugger
def directions():
print 'Welcome to the chem Balancer.'
print 'Use the following example to guide your work:'
global left #LEFT IS GLOBAL
left = 'B^6 + C^2 + B^3 + C^3 + H^9 + O^4 + Na^1'
print left
print "#Please note to use a 'hat' when entering all elements"
print '#use only one letter elements for now'
# left = raw_input('enter formula:') #enter formula to count
directions()
chem_stats = {}
chem_names = []
chem_names = []
chem_indy = []
for c in range(len(left)):
if left[c].isalpha() and left[c].isupper():
chars = ''
if left[c+1].islower():
chars += left[c]+left[c+1]
else:
chars += left[c]
#print chars
chem_indy.append(c)
chem_names.append(chars)
carrots = [x for x in range(len(left)) if left[x]=='^']
debug = Debugger(['carrots','chem_names','chem_indy','chem_stats']) # WITHOUT LEFT
debug.show()
Error message:
Traceback (most recent call last):
File "C:\Python27\#Files\repair\Chemical_Balancer.py", line 38, in <module>
debug.show()
File "C:\Python27\lib\site-packages\Debug.py", line 12, in show
print o,globals()[o]
File "<string>", line 1, in <module>
KeyError: 'carrots'
About the specific error on the left variable:
when you say a variable is global, python knows it has to look it up in the global namespace when its name is used. But in the code left hasn't been assigned in such namespace.
As you can see, left is commented out
#left = raw_input('enter formula:') #enter formula to count
Uncomment it by removing the # at the beginning of the line, so the line inside the directions function
global left
can find it and the instructions that follow can work.
About the implementation:
one solution to allow the debugger to know where to look for the variables, i.e. in which module, can be to provide the name of the module to it when it is created. Then the debugger object can reach the global variables of the module that created it via sys.modules[module_name].__dict__
debugger.py
import sys
class Debugger(object):
def __init__(self, module_name, objs):
assert type(objs)==list,'Not a list of strings'
self.objs = objs
self.module_name = module_name
def __repr__(self):
return '<class Debugger>'
def show(self):
for o in self.objs:
print o, sys.modules[self.module_name].__dict__[o]
chemical_balancer.py
import debugger as deb
a = 1
b = 2
d = deb.Debugger(__name__, ['a', 'b'])
print(d.objs)
d.show()
a = 10
b = 20
d.show()
which produces
['a', 'b']
a 1
b 2
a 10
b 20
As you can see, the debugger prints the current value of the variables each time its show method is called
I have found this SO Q&A informative and helpful.
I am trying to get myself familiar with decorators.
This is a program I created to do so, but it keeps giving me an TypeError: 'int' object is not callable error, which I don't know how to fix.
#Filename: decorator_practice.py
def add(x):
def add_1():
add_1 = x() + 1
return add_1
def minus(x):
def minus_1():
return x() - 1
return minus_1
def multi(x, times=2):
def multi_2():
return x() * 2
def multi_3():
return x() * 3
def multi_4():
return x() * 4
if times == 2:
return multi_2
elif times == 3:
return multi_3
elif times == 4:
return multi_4
else:
return "Please enter times between 2 and 4"
def create_x():
x = input('Give variable x a value: ')
return x
add(create_x()())
I run this and type: 5
Can anyone help me? Thanks!
Your create_x function returns an integer:
def create_x():
x = input('Give variable x a value: ')
return x
so create_x()() is never going to work.
Part of the problem is that you've used poor parameter names, which is confusing you - you have two xs which refer to two completely different things. Using your add decorator as an example, modify to:
def add(func):
def add_1():
return func() + 1 # you should return from the inner function
return add_1
Now hopefully it is clear that the argument to add should be a function, which is called inside add_1. Therefore you could do:
adder = add(create_x) # don't call create_x yet!
adder() # calling add_1, which calls create_x
which simplifies to:
add(create_x)() # note ordering of parentheses
Note that this could also be written:
#add
def create_x():
...
create_x()
where the syntax #add means create_x = add(create_x).
Once you've mastered simple decorators, note that your multi will not work as you expect - see e.g. python decorators with parameters for creating decorators that take arguments.
You have unnecessary (), change add(create_x()()) to add(create_x()),
and I suggest using x = int(raw_input('Give variable x a value: '))
See the following example:
def add(x):
def add_1():
#add_1 = x() + 1 # remove this line
return x+1
return add_1
def create_x():
x = input('Give variable x a value: ')
return x
b = add(create_x())
print 'answer: ', b()
localhost# python t.py
Give variable x a value: 5
answer: 6
I am trying to write a decorator that adds verbose logging to a function via a decorator (a method would be nice too, but I haven't tried that yet). The motivation behind this is that patching in a one-line add_logs decorator call into a box in production is much easier (and safer) than adding 100 debug lines.
Ex:
def hey(name):
print("Hi " + name)
t = 1 + 1
if t > 6:
t = t + 1
print("I was bigger")
else:
print("I was not.")
print("t = ", t)
return t
I'd like to make a decorator that will transform this into code that does this:
def hey(name):
print("line 1")
print("Hi " + name)
print("line 2")
t = 1 + 1
print("line 3")
if t > 6:
print("line 4")
t = t + 1
print("line 5")
print("I was bigger")
else:
print("line 6")
print("I was not.")
print("line 7")
print("t = ", t)
print("line 8")
return t
What I've got so far:
import inspect, ast
import itertools
import imp
def log_maker():
line_num = 1
while True:
yield ast.parse('print("line {line_num}")'.format(line_num=line_num)).body[0]
line_num = line_num + 1
def add_logs(function):
def dummy_function(*args, **kwargs):
pass
lines = inspect.getsourcelines(function)
code = "".join(lines[0][1:])
ast_tree = ast.parse(code)
body = ast_tree.body[0].body
#I realize this doesn't do exactly what I want.
#(It doesn't add debug lines inside the if statement)
#Once I get it almost working, I will rewrite this
#to use something like node visitors
body = list(itertools.chain(*zip(log_maker(), body)))
ast_tree.body[0].body = body
fix_line_nums(ast_tree)
code = compile(ast_tree,"<string>", mode='exec')
dummy_function.__code__ = code
return dummy_function
def fix_line_nums(node):
if hasattr(node, "body"):
for index, child in enumerate(node.body):
if hasattr(child, "lineno"):
if index == 0:
if hasattr(node, "lineno"):
child.lineno = node.lineno + 1
else:
# Hopefully this only happens if the parent is a module...
child.lineno = 1
else:
child.lineno = node.body[index - 1].lineno + 1
fix_line_nums(child)
#add_logs
def hey(name):
print("Hi " + name)
t = 1 + 1
if t > 6:
t = t + 1
print("I was bigger")
else:
print("I was not.")
print("t = ", t)
return t
if __name__ == "__main__":
print(hey("mark"))
print(hey)
This produces this error:
Traceback (most recent call last):
File "so.py", line 76, in <module>
print(hey("mark"))
TypeError: <module>() takes no arguments (1 given)
which makes sense because compile creates a module and of course modules are not callables. I've tried a hundred different ways of making this work at this point but can't come up with a working solution. Any recommendations? Am I going about this the wrong way?
(I haven't been able to find a tutorial for the ast module that actually modifies code at runtime like this. A pointer to such a tutorial would be helpful as well)
Note: I am presently testing this on CPython 3.2, but a 2.6/3.3_and_up solution would be appreciated. Currently the behavior is the same on 2.7 and 3.3.
When you compile the source, you get a code object representing a module, not a function. Substituting this code object into an existing function won't work, because it's not a function code object, it's a module code object. It's still a code object, though, not a real module, you so can't just do hey.hey to get the function from it.
Instead, as described in this answer, you need to use exec to execute the module's code, store the resulting objects in a dictionary, and extract the one you want. What you could do, roughly, is:
code = compile(ast_tree,"<string>", mode='exec')
mod = {}
exec(code, mod)
Now mod['hey'] is the modified function. You could reassign the global hey to this, or replace its code object.
I am not sure if what you're doing with the AST is exactly right, but you will need to do the above at any rate, and if there are problems in the AST manipulation, doing this will get you to a point where you can start to debug them.
It looks like you're trying to hackily implement a trace function. Can I suggest using sys.settrace to accomplish that in a more reusable fashion?
import sys
def trace(f):
_counter = [0] #in py3, we can use `nonlocal`, but this is compatible with py2
def _tracer(frame, event, arg):
if event == 'line':
_counter[0] += 1
print('line {}'.format(_counter[0]))
elif event == 'return': #we're done here, reset the counter
_counter[0] = 0
return _tracer
def _inner(*args, **kwargs):
try:
sys.settrace(_tracer)
f(*args, **kwargs)
finally:
sys.settrace(None)
return _inner
#trace
def hey(name):
print("Hi " + name)
t = 1 + 1
if t > 6:
t = t + 1
print("I was bigger")
else:
print("I was not.")
print("t = ", t)
return t
hey('bob')
Output:
$ python3 test.py
line 1
Hi bob
line 2
line 3
line 4
I was not.
line 5
t = 2
line 6
Note that the semantics of this are slightly different than in your implementation; the if branches not exercised by your code, for example, are not counted.
This ends up being less fragile - you're not actually modifying the code of the functions you're decorating - and has extra utility. The trace function gives you access to the frame object before executing each line of code, so you're free to log locals/globals (or do some dodgy injection stuff, if you're so inclined).
When you call inspect.getsource() with a decorated function, you also get the decorator, which, in your case, gets called recursively (just twice, and the second time it produces an OSError).
You can use this workaround to remove #add_logs line from the source:
lines = inspect.getsourcelines(function)
code = "".join(lines[0][1:])
EDIT:
It looks like your problem is that your dummy_function doesn't take arguments:
>>> print(dummy_function.__code__.co_argcount)
0
>>> print(dummy_function.__code__.co_varnames)
()
Whereas your original function does:
>>> print(hey.__code__.co_argcount)
1
>>> print(hey.__code__.co_varnames)
('name')
EDIT:
You're right about the code object being returned as a module. As pointed in another answer, you have to execute the this object and then, assign the resulting function (identifiable by function.__name__) to dummy_function.
Like so:
code = compile(ast_tree,"<string>", mode='exec')
mod = {}
exec(code, mod)
dummy_function = mod[function.__name__]
return dummy_function
Then:
>>> print(hey('you'))
line 1
Hi you
line 2
line 3
I was not.
line 4
t = 2
line 5
2
This question already has answers here:
I'm getting an IndentationError. How do I fix it?
(6 answers)
Closed 5 months ago.
I'm tasked to make a "Set" class that contains the variable self.list and be able to print and str() the object by writing the __repr__ and __str__ methods. A second file (driver1.py), a "driver file" creates a Set object and attempts to call print(str(set_object)) and print(set_object) but both calls only print a memory address, Set.Set instance at 0x1033d1488> or some other location. How do I change this? I want it to print out the contents of the set_object in the form {1,2,3}
Here is my code in it's entirety after updating indentation.
class Set:
def __init__(self):
self.list = []
def add_element(self, integer):
if integer not in self.list:
self.list.append(integer)
def remove_element(self, integer):
while integer in self.list: self.list.remove(integer)
def remove_all(self):
self.list = []
def has_element(self, x):
while x in self.list: return True
return False
#probably doesnt work, __repr__
def __repr__(self):
if self.list.len == 0:
return "{}"
return "{"+", ".join(str(e) for e in self.list) +"}"
#Same as above, probably doesnt work
def __str__(self):
if len(self.list) == 0:
return "{}"
return "{"+", ".join(str(e) for e in self.list) +"}"
def __add__(self, other):
counter = 0
while counter <= len(other.list):
if other.list[counter] not in self.list:
self.list.append(other.list[counter])
counter = counter + 1
Why do I get the error:
Traceback (most recent call last):
File "driver1.py", line 1, in <module>
from Set import *
File "/Users/josh/Documents/Set.py", line 23
return "{"+", ".join(str(e) for e in self.list) +"}"
^
IndentationError: unindent does not match any outer indentation level
You've mixed tabs and spaces. Don't do that; this is what happens when you do. Python thinks some of your methods are actually internal to some of your other methods, so the Set class doesn't actually have __str__ or __repr__ methods.
Fix your indentation, and your problem will go away. To avoid such problems in the future, turn on "show whitespace" in your editor, and try running Python with the -tt command line option if you think you might be seeing tab-related bugs.
There is another problem at:
if self.list.len == 0:
you probably meant to do:
if len(self.list) == 0:
Once this issue is fixed, the code works:
s = Set()
s.add_element(1)
s.add_element(1)
s.add_element(2)
s.add_element(3)
print s # prints {1, 2, 3}