Python function with excessive number of variables [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 20 days ago.
Improve this question
Very general question: I am attempting to write a fairly complext Python script.
#Part A: 100 lines of python code
#Part B: 500 lines of python code
#Part C: 100 lines of python code
Assume that I want "Part B" taken out of the picture for readability and debugging purposes, because I know that it is running well and I want to focus on the other parts of the code.
I would define a function like this:
def part_b():
#500 lines of python code
#Part A: 100 lines of python code
part_b()
#Part C: 100 lines of python code
The problem with this approach in my case is that there are more than twenty variables that need to be sent to "Part C". The following looks like bad practice.
def part_b():
global var1
global var2
global var3...
I am aware that I could return an object with more than twenty attibutes, but that would increase complexity and decrease readability.
In other words, is there a pythonic way of saying "execute this block of code, but move it away from the code that I am currently focusing on". This is for a Selenium automation project.

Sounds like what you're looking for is modules. Move part B into a separate file, and then import it.
import part_B
# part A here
part_B.run()
# part C here
# to access things that part B does:
do_part_C_stuff(part_B.something)

Related

Python get value of variable from RAM [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Is it possible, using Python to get a value of a variable from RAM using an address, something like Cheat Engine?
It's really implementation dependent, you could try this in CPython with ctypes.string_at function (it dumps whole Python3 integer structure):
import ctypes
from sys import getsizeof
my_value_1 = int(1)
my_value_2 = int(2)
b1 = ctypes.string_at(id(my_value_1), getsizeof(my_value_1))
b2 = ctypes.string_at(id(my_value_2), getsizeof(my_value_2))
print(hex(id(b1)), ''.join(format(x, '02x') for x in b1))
print(hex(id(b2)), ''.join(format(x, '02x') for x in b2))
Prints:
0x7ffff670ffb0 4d0300000000000040b89d0000000000010000000000000001000000
0x7ffff670f5f0 740000000000000040b89d0000000000010000000000000002000000
In other implementations of Python (Jython, Iron Python etc.) this probably won't work. On CPython id() returns memory address of the object.
I found a Python module hackManager, and it solved my issue.

Solve for x in python without Scipy, [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to solve for an equation in Python without using any scipy features. c = 5 and the equation is c = 10 - 20(exp(-0.15*x) - exp(-0.5*x)).
How do I solve for x with a tolerance of .0001.
Pardon my intro level programming here guys. This is the first class I've ever taken.
from math import exp
c = 5
def x(c):
c = 10 - 20(exp*(-0.15*x) - exp*(-0.5*x))
return x(5)
You might want to have a look at SymPy. It's a dedicated algebraic symbol manipulation library for Python with a BSD license. If you're looking for a "stock"/standard library solution, then as others have mentioned you're going to have to do some homework and potentially implement your own solver.
As a closing thought, unless this is a class assignment or your boss has a pathological hatred of third-party open source libraries, there's really no good reason not to use one of the SciPy packages. IIRC, they're largely implemented as highly-optimized C binaries wrapped in Python modules, so you get blazingly fast performance and the ease-of-use of a Python API.
It seems like you want to implement this "from scratch." A few hints:
We can simplify this a bit with algebra. What you really want is to find x such that exp(-0.15*x) + exp(-0.5*x) - 0.2 = 0
For a given value of x, you know how much error you have. For example, if x = 1, then c(1) = 1.267, so your error is 1.267. You need to keep "guessing" values until your error is less than 0.0001.
Math tells us that this function is monotonically decreasing; so, there is no point checking answers to the left of 1.
Hopefully you can solve it from these hints. But this is supposed to be an answer, so here is the code:
def theFunction(x): return exp(-0.15*x) + exp(-0.5*x) - 0.2
error = 1.267
x = 1
littleBit = 1
while (abs(error) > 0.0001):
if error > 0: x += littleBit
else: x -= littleBit
oldError = error
error = theFunction(x)
if (error*oldError < 0): littleBit *= 0.5
print x
Note, the last three lines in the loop are a little bit 'clever' -- an easier solution would be to just set littleBit = 0.00001 and keep it constant throughout the program (this will be much slower, but will still do the job). As an exercise, I recommend trying to implement it this simpler way, then time how long it takes both ways, and see if you can figure out where the time savings comes in.

Python Function Won't Run At ALL? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am fairly new to the programming world and am wondering why the following code will not only refuse to run, but my python software won't even give me any error messages whatsoever.I'm using Pythonista, an IOS app for python. I can't get the app to run this code (and it won't give me any error messages) and was wondering if it's my code itself, or is it just the application. Any knowledge on this issue would be greatly appreciated.
def starBits():
badMatchups = [Zelda, Cloud, Ryu]
worstMatchups = [Jigglypuff, Villager, Bayonetta]
print(badMatchups)[1:2]
print(worstMatchups)[1:1]
def main():
starBits()
main()
I'm not sure what you expect from this, but it's really funky syntax.
print(badMatchups)[1:2]
print(worstMatchups)[1:1]
If those slices are subscripts for the lists, you need them inside the call to print:
print(badMatchups[1:2])
print(worstMatchups[1:1])
By the way, do you realize that [1:1] is an empty slice? The second number is the first position not included. You may need
print(badMatchups[1:3]) # two elements
print(worstMatchups[1:2]) # one element
Also, are those elements external variables, or are they supposed to be literal names? If the latter, then you have to put them in quotation marks.
badMatchups = ["Zelda", "Cloud", "Ryu"]
worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]
With this change, the code runs; I hope it's what you want.
Can't get it to run? Reality check time ...
Full code, changes made:
def starBits():
badMatchups = ["Zelda", "Cloud", "Ryu"]
worstMatchups = ["Jigglypuff", "Villager", "Bayonetta"]
print(badMatchups[1:3])
print(worstMatchups[1:2])
def main():
starBits()
main()
Output:
['Cloud', 'Ryu']
['Villager']

Python 2.7 IndentationError [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am getting an IndentationError when trying to run my program in a Python Interpreter:
line 127
global map
^
IndentationError: expected an indented block
I am using python version 2.7
What's wrong with the following code?:
def make_map():
global map
Python expects 4 spaces or a tab to indent and align code - similar to Java expecting curly {} brackets are the start of a loop, method or class etc.
def some_function():
somecode
morecode
...
should be formatted as
def some_function():
somecode
morecode
...
It appears that your code throws an exception on line 127, so check this and indent the code as required.
def some_code():
for i in range(1, some_value):
some_method()
if need_more_indent:
indent_code()
do_this_after_indent_code()
this_runs_after_for_loop()
return 'lol'

another Python indentation issue [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
for reason, now my code has some weird formatting issue.
if __name__ == "__main__":
import doctest
# ### UNCOMMENT THESE ONE AT A TIME TO GET THE TESTS TO PASS.
# ### WHEN YOU ARE FINISHED, LEAVE ONLY THE "allTests.txt" line uncommented.
# doctest.testfile("noConsecDupsTests.txt", verbose=True)
# doctest.testfile("isVowelTests.txt", verbose=True)
# doctest.testfile("countVowelsTests.txt", verbose=True)
# doctest.testfile("allVowelsATests.txt", verbose=True)
# doctest.testfile("syllableHelperTests.txt", verbose=True)
# doctest.testfile("removeSilentETests.txt", verbose=True)
# doctest.testfile("removeEdWhenNotASyllableTests.txt", verbose=True)
# doctest.testfile("countSyllablesTests.txt", verbose=True)
doctest.testfile("allTests.txt", verbose=True)
Python 3.3.2 is saying that the indentation error is in the first line of the code... I'm really confused. I've never been so stumped on such a simple topic.
I would guess that you are trying to run this in the Python REPL? As opposed to saving it to a file and then running it?
Python REPL ends a code block after an empty line. Try saving this in a file, say test.py and then call it from command line using python test.py.

Categories