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.
Related
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)
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 4 years ago.
Improve this question
My C++ is functioning as expected but the equivalent Python code hangs in an infinite loop. Help!
C++
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<4;++i){
int j=0;
while(i!=j){
++j;
cout<<j<<endl;
}
}
}
Python
for i in range(4):
j = 0
while i != j:
++j
print(j)
++j is not a thing in Python. You want j += 1.
In order to avoid ambiguity/confusion, our Benevolent Dictator For Life thought to not allow ++ or -- into the Python ecosystem. That means you are in an infinite loop because ++j does not do what you believe it to do.
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.
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'
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 6 years ago.
Improve this question
What is the best way to mark deprecated fields in Django 1.4 models if I don't want to delete them right away?
Sorry, I can't comment yet.
If you don't want anything fancy you could simply compare the Python version and launch a warning.
import django
print django.VERSION
>> (1, 8, 5, 'final', 0)
if django.VERSION[1] < 4:
print "[DEPRECATION WARNING]"
Or you could do the best approach: to go to a popular package and see how they do it. For example in Django CMS:
cms/exceptions.py (https://github.com/divio/django-cms/blob/develop/cms/exceptions.py)
# -*- coding: utf-8 -*-
class Deprecated(Exception): pass
cms/utils/check.py (https://github.com/divio/django-cms/blob/develop/cms/utils/check.py)
#define_check
def check_deprecated_settings(output):
with output.section("Deprecated settings") as section:
found = False
for deprecated in ['CMS_FLAT_URLS', 'CMS_MODERATOR']:
if hasattr(settings, deprecated):
section.warn("Deprecated setting %s found. This setting is no longer in use and can be removed" % deprecated)
found = True
if not found:
section.skip("No deprecated settings found")