Python Forum
two functions working in a strange way
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
two functions working in a strange way
#1
I am meeting a problem with 2 functions
The 1st one
the first function job: clean a string and
at the end, it calls the function def pi_digits():
in order to recover the value of pi .
This sequence gives a message <generator object pi_digits at 0x00000223B0738580> 
which I cannot manage

[python]
#---------------------------------- init  
mytestr = "Afdc ERFGDS 12547 éàè,;:!) "
fenet_1.insert("1.0",mytestr)
DIGITS = 200
drapo_trim = False
#==================

def trim() :#-----------------------------------------------------nettoyer la chaine vers concatené
        source=fenet_1.get("1.0", 'end-1c')
#---------------ponctu    
        spunct = re.sub(r'[^\w\s]', '', source)                
        fenet_2.insert("1.0",spunct)
#--------------- accents
        aks = unidecode.unidecode(spunct)       
#--------------- maju
        majstr=aks.upper()      
#--------------- ote chiffres
        res = ''.join([i for i in majstr if not i.isdigit()])       
#--------------  Concatene ote espaces
        resf = res.replace(' ','')
        fenet_2.delete("1.0","end")
        fenet_2.insert("1.0",resf) 
        #finex pour- resf - chaine ok
                                   # on developpe la chaine -texte plain
        l_resf =len(resf)# calcul de sa longueur

        bin_res = ''.join(format(ord(i), '08b') for i in resf)  # on convertit en binaire
        print((bin_res) , " print du texte clair en binaire - ligne 99")   #   binaire du texte clair  idem
        drapo_trim = True
        bin_pi=pi_digits()
        print ((bin_pi), "retour de fn")

#================================================================

def pi_digits():
    print("into pi_digits")
    k,a,b,a1,b1 = 2,4,1,12,4
    while x > 0:
        p,q,k = k * k, 2 * k + 1, k + 1
        a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
        d,d1 = a/b, a1/b1
        while d == d1 and x > 0:
            yield int(d)
            x -= 1
            a,a1 = 10*(a % b), 10*(a1 % b1)
            d,d1 = a/b, a1/b1
    digits = [str(n) for n in list(pi_digits(DIGITS))]
    str_pi='{}.{}'.format(digits.pop(0), "".join(digits))
    context = decimal.Context(prec=100)
    decimal.setcontext(context)
    pi = decimal.Decimal(str_pi)
#     return pi.lstrip(pi[0:4])
##    print((pi) , " = pi en binaire - lig 123") 
    return pi
# the return gives
# <generator object pi_digits at 0x00000223B0738580>
However the 'creation function of pi' as follow is working alone and ives
the right value - who has a clue
many thanks?
#-------------------------------- 
from tkinter import *
import tkinter  as tk
from unidecode import unidecode
from tkinter import filedialog,END 
from tkinter.filedialog import asksaveasfilename, askopenfilename
from tkinter.ttk import Label, LabelFrame
from PIL import Image, ImageTk
from tkinter import messagebox
from tkinter.scrolledtext import ScrolledText
import decimal
import unidecode
import re

root = tk.Tk()

DIGITS = 200

def pi_digits(x):
    k,a,b,a1,b1 = 2,4,1,12,4
    while x > 0:
        p,q,k = k * k, 2 * k + 1, k + 1
        a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
        d,d1 = a/b, a1/b1
        while d == d1 and x > 0:
            yield int(d)
            x -= 1
            a,a1 = 10*(a % b), 10*(a1 % b1)
            d,d1 = a/b, a1/b1

digits = [str(n) for n in list(pi_digits(DIGITS))]
str_pi='{}.{}'.format(digits.pop(0), "".join(digits))

context = decimal.Context(prec=100)
decimal.setcontext(context)
pi = decimal.Decimal(str_pi)
print(pi)

#-------------------------------- bouton go
button1 = Button(root, text = "go ", width=8, height=1, fg='white', font=('Helvetica 12 bold'),
                background="#762123" ,command=pi_digits)
button1.place(x=740, y=36)
#-------------------------------- 
hope you undersatand my problem
many thanks
Reply
#2
You changed the function pi_digits.

When running alone, you wrote pi_digits like this:
def pi_digits(x):
    k,a,b,a1,b1 = 2,4,1,12,4
    while x > 0:
        p,q,k = k * k, 2 * k + 1, k + 1
        a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
        d,d1 = a/b, a1/b1
        while d == d1 and x > 0:
            yield int(d)
            x -= 1
            a,a1 = 10*(a % b), 10*(a1 % b1)
            d,d1 = a/b, a1/b1
When combined with the other function you added code to the bottom of the function.
def pi_digits():
    print("into pi_digits")
    k,a,b,a1,b1 = 2,4,1,12,4
    while x > 0:
        p,q,k = k * k, 2 * k + 1, k + 1
        a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
        d,d1 = a/b, a1/b1
        while d == d1 and x > 0:
            yield int(d)
            x -= 1
            a,a1 = 10*(a % b), 10*(a1 % b1)
            d,d1 = a/b, a1/b1
    digits = [str(n) for n in list(pi_digits(DIGITS))]
    str_pi='{}.{}'.format(digits.pop(0), "".join(digits))
    context = decimal.Context(prec=100)
    decimal.setcontext(context)
    pi = decimal.Decimal(str_pi)
#     return pi.lstrip(pi[0:4])
##    print((pi) , " = pi en binaire - lig 123") 
    return pi
This code does not belong in pi_digits. This code uses pi_digits to create a decimal pi.
    digits = [str(n) for n in list(pi_digits(DIGITS))]
    str_pi='{}.{}'.format(digits.pop(0), "".join(digits))
    context = decimal.Context(prec=100)
    decimal.setcontext(context)
    pi = decimal.Decimal(str_pi)
#     return pi.lstrip(pi[0:4])
##    print((pi) , " = pi en binaire - lig 123") 
    return pi
You could write a new function that uses pi_digits and returns pi with the specified number of digits.
import decimal

def pi_digits(x):
    """Generates digits for pi."""
    k,a,b,a1,b1 = 2,4,1,12,4
    while x > 0:
        p,q,k = k * k, 2 * k + 1, k + 1
        a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
        d,d1 = a/b, a1/b1
        while d == d1 and x > 0:
            yield int(d)
            x -= 1
            a,a1 = 10*(a % b), 10*(a1 % b1)
            d,d1 = a/b, a1/b1

def decimal_pi(precision):
    """Return pi to the specified precision."""
    digits = [str(n) for n in list(pi_digits(precision))]
    str_pi='{}.{}'.format(digits.pop(0), "".join(digits))
    
    context = decimal.Context(prec=100)
    decimal.setcontext(context)
    return decimal.Decimal(str_pi)

print(decimal_pi(200))
Or you can integrate the generator if it isn't used elsewhere.
def decimal_pi(precision):
    """Calculate pi to precision number of digits."""
    x = precision
    digits = []
    k, a, b, a1, b1 = 2, 4, 1, 12, 4
    while x > 0:
        p, q, k = k * k, 2 * k + 1, k + 1
        a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1
        d, d1 = a / b, a1 / b1
        while d == d1 and x > 0:
            digits.append(str(int(d)))
            x -= 1
            a, a1 = 10 * (a % b), 10 * (a1 % b1)
            d, d1 = a / b, a1 / b1
    digits.insert(1, ".")
    decimal.setcontext(decimal.Context(prec=precision))
    return decimal.Decimal("".join(digits))
Reply
#3
hello deanhystad
Sorry for my late reply
Many thanks for the help you provide !
I have understood your explications and am completing my project...
good luck
mik
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Does anyone have unicurses panel functions working on a Windows 10 platform? pjfarley3 0 1,496 Oct-11-2020, 04:41 AM
Last Post: pjfarley3
  Called Functions Not Working WhatAmIDoing09 3 2,613 Jul-12-2019, 07:02 PM
Last Post: ichabod801
  Working on nested functions boxerboy1168 2 2,649 Dec-28-2018, 07:54 AM
Last Post: Gribouillis
  Built in Functions not working mhoneycutt83 4 6,433 Jul-28-2017, 07:39 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020