Re: Access a class in another python script
On Wednesday, October 7, 2015 at 11:46:05 PM UTC-7, Terry Reedy wrote: > On 10/8/2015 2:24 AM, DBS wrote: > > I'm using Python 3.5 and have two python scripts where one needs > > access to a class in the other script for authentication purposes. > > Any python .py file can be either run as a main program or module (ie, > script) or imported as a module by another module. If a file is meant > for both, it should end with > > if __name__ == '__main__: > > > The latter is ofter to call a function main() defined previously. If > you have a problem with this, write a **minimal** example with at most > 10 lines per file and post. > > > -- > Terry Jan Reedy Hello Terry, Thanks. Will do and get back with what come up with. To clarify your repsonse, I need to change the file name to reflect __filename__.py and then import the needed components (classes and variables) accordingly? -- https://mail.python.org/mailman/listinfo/python-list
Access a class in another python script
Hello, I'm using Python 3.5 and have two python scripts where one needs access to a class in the other script for authentication purposes. The scripts runs through GitHub to poll for all pull requests and pull requests that meet a certain condition. The call from the second script to the class and variables in the first script are working, but I'm getting prompted for credentials several times. I wrapped the credentials in a class and created an object in the second script to access the class so that I would not be prompted for credentials in the second script. I've include both scripts below. I'm still new to python, but I think it's how I'm calling the class or the import in the second script??? Thanks in advance for your time. main_en_pr script: #! /usr/bin/python import os import github3 from github3 import login, GitHub, authorize from getpass import getuser, getpass import requests import csv import configparser import sys import datetime import codecs sys.__stdout__ = codecs.getwriter('utf8')(sys.stdout) # Class to authenticate to GitHub class GitAuth: gh = None def authentication(self): try: user = input('GitHub username: ') except KeyboardInterrupt: user = getuser() password = getpass('GitHub token for {0}: '.format(user)) self.gh = login(user, password) return user # Assign the class to an object myobjectx = GitAuth() # Assign the variable user to the function inside the class user = myobjectx.authentication() # Read the contents of the config file to pull in the repo name config = configparser.ConfigParser() config.read('repo.ini') repo = config.get('repos', 'repo1') result = myobjectx.gh.repository(user, repo).pull_requests('open') # Define function to list all pull requests def list_all_prs(): # open csv file and create header rows with open('c:\\pull.csv', 'w+', newline='') as f: csv_writer = csv.writer(f) csv_writer.writerow(['Id', 'Login', 'Title', 'Commits', 'Changed Files']) # iterate through repo for pull requests based on criteria and output to csv file for pr in result: data = pr.as_dict() changes = (myobjectx.gh.repository(user, repo).pull_request(data['number'])).as_dict() # keep print to console statement for testing purposes # print(changes['id'], changes['user']['login'], changes['title'], changes['commits'], changes['changed_files']) with open('c:\\pull.csv','a+',newline='') as f: csv_writer = csv.writer(f) csv_writer.writerow([changes['id'], changes['user']['login'], changes['title'], changes['commits'], changes['changed_files']]) list_all_prs() # Call the validation script exec(open("one_commit_one_file_change.py").read()) +++ one_commit_one_file_change script: #! /usr/bin/python import os import github3 from github3 import login, GitHub, authorize from getpass import getuser, getpass import requests import csv import configparser import sys import main_en_pr from main_en_pr import GitAuth import codecs sys.__stdout__ = codecs.getwriter('utf8')(sys.stdout) myobjecty = GitAuth() user = myobjecty.authentication() def one_commit_one_file_change_pr(): #open csv file and create header rows with open('c:\\commit_filechange.csv', 'w+') as f: csv_writer = csv.writer(f) csv_writer.writerow(['Login', 'Title', 'Commits', 'Changed Files','Deletions', 'Additions']) #iterate through repo for pull requests based on criteria and output to csv file for pr in main_en_pr.result: data = pr.as_dict() changes = (myobjecty.gh.repository(user, main_en_pr.repo).pull_request(data['number'])).as_dict() if changes['commits'] == 1 and changes['changed_files'] == 1: #keep print to console statement for testing purposes #print changes['user']['login'] with open('c:\\commit_filechange.csv', 'a+') as f: csv_writer = csv.writer(f) csv_writer.writerow([changes['user']['login'], changes['title'], changes['commits'], changes['changed_files']]) one_commit_one_file_change_pr() -- https://mail.python.org/mailman/listinfo/python-list
Re: Python/Github
On Monday, August 24, 2015 at 2:28:39 PM UTC-7, DBS wrote: > On Monday, August 24, 2015 at 2:16:55 PM UTC-7, Chris Rebert wrote: > > On Mon, Aug 24, 2015 at 1:14 PM, DBS wrote: > > > Hello, > > > > > > I'm trying to retrieve the number of commits and changed files on all > > > pull requests submitted to a branch. > > > > > > The call to get all the pull requests is GET /repos/:owner/:repo/pulls, > > > but it does not return "commits" or "changed_files". > > > > > > The call that returns number of commits and changed files is GET > > > /repos/:owner/:repo/pulls/:number. > > > > > > Any guidance on how I can modify the call below to include commits and > > > changed files on all pull requests? > > > > > > #!/usr/bin/env python > > > import json > > > import requests > > > > > > Have you considered using a GitHub API Python library such as > > github3.py (https://github.com/sigmavirus24/github3.py ) rather than > > reinventing the wheel? > > > > Cheers, > > Chris > > -- > > http://chrisrebert.com > > Hello Chris, > > No I haven't. I didn't know it existed until now. I'm still in the midst of > learning python. I will head over that way and read up on the documentation > and give it a try. If I have any questions, I will come back and post :). > Thanks again!! > > Thanks, > DBS On Monday, August 24, 2015 at 2:28:39 PM UTC-7, DBS wrote: > On Monday, August 24, 2015 at 2:16:55 PM UTC-7, Chris Rebert wrote: > > On Mon, Aug 24, 2015 at 1:14 PM, DBS wrote: > > > Hello, > > > > > > I'm trying to retrieve the number of commits and changed files on all > > > pull requests submitted to a branch. > > > > > > The call to get all the pull requests is GET /repos/:owner/:repo/pulls, > > > but it does not return "commits" or "changed_files". > > > > > > The call that returns number of commits and changed files is GET > > > /repos/:owner/:repo/pulls/:number. > > > > > > Any guidance on how I can modify the call below to include commits and > > > changed files on all pull requests? > > > > > > #!/usr/bin/env python > > > import json > > > import requests > > > > > > Have you considered using a GitHub API Python library such as > > github3.py (https://github.com/sigmavirus24/github3.py ) rather than > > reinventing the wheel? > > > > Cheers, > > Chris > > -- > > http://chrisrebert.com > > Hello Chris, > > No I haven't. I didn't know it existed until now. I'm still in the midst of > learning python. I will head over that way and read up on the documentation > and give it a try. If I have any questions, I will come back and post :). > Thanks again!! > > Thanks, > DBS Hello again, After playing around with the github3.py library, I'm pretty impressed. I do have another question. If I make the request to list the pull requests in my repo with the code below, I get just the name of the pull request. When I use the native get call exposed by the GitHub API, it returns JSON data that I can iterate through. Any clues on how to return JSON using the library. I've imported the JSON module, but I'm not having any luck in getting JSON returned. #!/usr/bin/env python from pygithub3 import Github import requests import json import sys auth = dict(login="x",user="x",token="xxx",repo="my-repo") gh = Github(**auth) pr = gh.pull_requests.list().all() print pr -- https://mail.python.org/mailman/listinfo/python-list
Re: Python/Github
On Monday, August 24, 2015 at 2:16:55 PM UTC-7, Chris Rebert wrote: > On Mon, Aug 24, 2015 at 1:14 PM, DBS wrote: > > Hello, > > > > I'm trying to retrieve the number of commits and changed files on all pull > > requests submitted to a branch. > > > > The call to get all the pull requests is GET /repos/:owner/:repo/pulls, but > > it does not return "commits" or "changed_files". > > > > The call that returns number of commits and changed files is GET > > /repos/:owner/:repo/pulls/:number. > > > > Any guidance on how I can modify the call below to include commits and > > changed files on all pull requests? > > > > #!/usr/bin/env python > > import json > > import requests > > > Have you considered using a GitHub API Python library such as > github3.py (https://github.com/sigmavirus24/github3.py ) rather than > reinventing the wheel? > > Cheers, > Chris > -- > http://chrisrebert.com Hello Chris, No I haven't. I didn't know it existed until now. I'm still in the midst of learning python. I will head over that way and read up on the documentation and give it a try. If I have any questions, I will come back and post :). Thanks again!! Thanks, DBS -- https://mail.python.org/mailman/listinfo/python-list
Python/Github
Hello, I'm trying to retrieve the number of commits and changed files on all pull requests submitted to a branch. The call to get all the pull requests is GET /repos/:owner/:repo/pulls, but it does not return "commits" or "changed_files". The call that returns number of commits and changed files is GET /repos/:owner/:repo/pulls/:number. Any guidance on how I can modify the call below to include commits and changed files on all pull requests? #!/usr/bin/env python import json import requests import datetime import sys import codecs sys.stdout = codecs.getwriter('utf8')(sys.stdout) sys.stderr = codecs.getwriter('utf8')(sys.stderr) OAUTH_KEY = "xxx" repos = ['my-repo'] # List pull request def list_pr(): for repo in repos: r = requests.get('https://api.github.com/repos/owner/%s/pulls' % repo, auth=('token', OAUTH_KEY)) data = r.json() for i in data: print "\nUser: " + i['user']['login'] + '\n' + "Number: " + str(i['number']) + '\n' + "Time created: " + i['created_at'] + '\n' + "Title: " + i['title'] + '\n' + "Body: " + i['body'] + '\n' list_pr() -- https://mail.python.org/mailman/listinfo/python-list