0

This might be a stupid question, but here going nothing:

my counter starts at 2821614688517316738.

How can I add 1 to each for each iteration?

counter = 2821614688517316738
counter += 1
print(counter)

output:

2821614688517316739
2821614688517316739
2821614688517316739

How do I add 1 to it continuously, and not revert back to the original counter number?

Some of you asked for the full code, here it is.

Brief eacxplanation there are 3954 documents that it goes through. It connects to an API which required the counter to +1 for each iteration or request sent to API.

from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
import glob
import os
import hashlib 
import hmac 
import requests 
import json
import pandas as pd
from pandas.io.json import json_normalize


path = "/Users/User/Downloads/Thesis papers/links/"
for filename in glob.glob(os.path.join(path, "*")):
    with open(filename) as open_file:
        content = open_file.read()

    bs = BeautifulSoup(content, "xml")
    for individual_xml in bs.find_all("Response"):
        for link in individual_xml.find_all("Fields"):
            for fields in link.find_all("Field"):
                word = "Earnings Call"
                if word in fields["value"]:

                    for i in link.find_all("Field", {"id":"7011"}):
                        #print(fields)
                        #print(i["value"][0])


                        #Your FactSet Information  
                        key = ' *' #Insert Key from auth-factset.com 
                        keyId = '*' #Insert KeyID from auth-factset.com 
                        username = '*'  #Insert your FactSet Username provided by your FactSet Account team 
                        serial = '*' #Insert Serial Number tied to machine account provided by your FactSet Account 
                        counter = 2821614688517316742
                        for gg in range(counter):
                            counter += 1
                            print(counter)
                            ba_key = bytearray.fromhex(key) 
                            my_int = counter.to_bytes(8, 'big', signed=True) 
                            my_hmac = hmac.new(ba_key,msg=my_int, digestmod=hashlib.sha512) 
                            digested_counter = my_hmac.digest() 
                            otp = digested_counter.hex()
                            json_object = {     
                                    'username': username,     
                                    'keyId': keyId,     
                                    'otp': otp ,     
                                    'serial': serial 
                                    }
                            OTP_url = 'https://auth.factset.com/fetchotpv1' 
                            payload = json.dumps(json_object) 
                            header = {'Content-Type': 'application/json'} 
                            r = requests.post(OTP_url, data=payload)   
                            r_key = r.headers.get(key='X-DataDirect-Request-Key')
                            r_token = r.headers.get(key='X-Fds-Auth-Token') 
                            print('DataDirect Request Key: ', r_key) 
                            print('Token:', r_token) 
                            #Confirm authentication and session token work 
                            header = {'X-Fds-Auth-Token':r_token}  
                            Service_url = 'https://datadirect.factset.com/services/auth-test'
                            r = requests.get(Service_url,headers=header) 
                            url = i["value"]
                            r = requests.get(url,headers=header)
                              #bs = BeautifulSoup(r, "xml")

                            #print(r.text)
                            with open(''+fields["value"]+''+'.xml', 'w') as f:
                                f.write(r.text)
doomdaam
  • 691
  • 1
  • 6
  • 21
  • 1
    "for each iteration in my loop" - what loop are you talking about? – ForceBru Mar 12 '20 at 11:43
  • It's part of larger code. But it is not necessary for the counter. See my update as another example. – doomdaam Mar 12 '20 at 11:44
  • Looks like you're defining `counter = 2821614688517316738` _in the loop_, which will reset `counter` to that constant on each iteration – ForceBru Mar 12 '20 at 11:46
  • How many 1s should you add? What is the stop condition of the loop? – Mayhem Mar 12 '20 at 11:46
  • exactly. Not sure how to fix this. – doomdaam Mar 12 '20 at 11:47
  • The big code does matter. If you set the counter to be ```counter = 2821614688517316738``` every iteration of the big loop then after adding +1 it will always stay the same. You should take that part outside the loop. – Ohad Mar 12 '20 at 11:47
  • @doomdaam, it's as simple as moving `counter = 2821614688517316738` outside the loop – ForceBru Mar 12 '20 at 11:47
  • This loop: `counter = 2821614688517316742; while counter > 2821614688517316742:` will never be executed. You set `counter = 2821614688517316742` and then immediately check if it's greater than 2821614688517316742. Well, that's never going to happen, so `counter > 2821614688517316742` will be `False`, and the `while` loop won't run – ForceBru Mar 12 '20 at 11:50
  • Here's what I'm thinking. To move the counter outside the full code and add one to it, until it reaches counter + 3954, which is the number of documents I have. – doomdaam Mar 12 '20 at 11:56
  • No solution has been posted yet. – doomdaam Mar 12 '20 at 12:44

2 Answers2

2

simple answer:

counter = 2821614688517316740
while True:
    counter += 1
    print(counter)

just beware that this is an infinite loop...

EDIT

I know the exact number of iterations required, it is 3954, can we set this as a limit somehow? –

Of course.

counter = 2821614688517316740
for _ in range(3954):
    counter += 1
    print(counter)

Now may I kindly suggest you do the full official Python tutorial ?

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
0

It will iterate through counter and keep adding a number in it

counter = 2821614688517316738
for i in range(3954):
    counter += 1
    print(counter)
Hamad Javed
  • 106
  • 6
  • 1
    No, it won't be an infinite loop: it'll stop as soon as `i` becomes equal to `282161...` because `range(...)` is explicitly bounded – ForceBru Mar 12 '20 at 11:58