C++ vs Python implementation [closed] - python

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.

Related

Python function with excessive number of variables [closed]

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)

Please Explain me Below lines with code?(Recursion In Python) [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 3 years ago.
Improve this question
define function, speller
if there are no more letters
print "all done"
print the first letter
invoke speller with the given name minus the first letter
If we invoked this function with “Zoe” as the argument, we would see “Z”, “o”, and “e” printed out before “all done”.
We call the function a total of 4 times!
function called with “Zoe”
function called with “oe”
function called with “e”
function called with “”
Recursive means the function should call itself from within the method:
def speller(word):
if not word:
print('all done')
return
print(word[0])
speller(word[1::])
speller('Zoe')
Output:
>> Python 3.6.8 (default, Jun 11 2019, 01:21:42)
>> [GCC 6.3.0 20170516] on linux
>> Z
>> o
>> e
>> all done
https://repl.it/repls/CoolAmusedPlots

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.

How to mark deprecated fields in Django 1.4? [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 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")

high precision (~200 sig figs) incomplete gamma function callable in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
im wondering if anyone knows of any libraries that allow for very high precision (200+ sig figs, preferably just arbitrary) incomplete gamma functions? So far the only thing i've found is mpmath for python, but I have no idea how I would incorporate that into C code, if that is even possible.
It could be in any language, just so long as I can somehow link it and call it from C.
Cheers
I think the GNU MP Bignum library is exactly what you're looking for.
You can do things like:
#include <stdio.h>
#include <gmp.h>
int main(int argc, char *argv[]) {
mpz_t a, b, c;
if (argc&lt3) {
printf("Please supply two numbers to add.\n");
return 1;
}
mpz_init_set_str (a, argv[1], 10);
mpz_init_set_str (b, argv[2], 10);
mpz_add (c, a, b);
printf("%s + %s => %s\n", argv[1], argv[2], mpz_get_str (NULL, 10, c));
return 0;
Compile with: gcc -o add_example add_example.c -lgmp -lm
As an alternative route, you could use a mathematical application that supports running in batch like Maxima, Mathematica or Matlab.
I tried the following Maxima script:
fpprec : 200;
fpprintprec : 200;
bfloat(gamma_incomplete(2,bfloat(2.3)));
Running it like this: maxima -q -b ./incompletegamma.mc run gives:
(%i1) batch("./gamma.mc")
read and interpret file: ./gamma.mc
(%i2) fpprec:200
(%o2) 200
(%i3) fpprintprec:1000
(%o3) 1000
(%i4) bfloat(gamma_incomplete(2,bfloat(2.3)))
(%o4) 3.3085418428525236227076532372691440857133290256546276037973522730841281\
226873248876116604879039570340226922621099906787322361219389317316508680513479\
116358485422369818232009561056364657588558639170139b-1
You can use system() or call() to run the maxima script from a python application like this:
from subprocess import call
call(["maxima", "-q -b ./incompletegamma.mc run"])

Categories