Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

This code is for part of my first major contribution to open-source, for the socli project. The purpose is to allow the user to select a site from the stack exchange network to browse and fill two variables with the appropriate urls (used through the rest of the program). Since the stack exchange network is adding new sites all the time, I wanted to make it decently easy to update the stored list. Also, I'm lazy, and didn't want to manually create a dictionary =) So I use a text file with the list of sites copy-pasted into it and the code takes care of the rest.

##Approach

Approach

The purpose of the code is to go through a text file named sites.txt filled with urls in a format like

https://codereview.stackexchange.com
https://physics.stackexchange.com

Taking the above, it would then produce a dictionary looking like

{'codereview':'https://codereview.stackexchange.com', 'physics':'https://physics.stackexchange.com'}

It would also print

Sites: codereview, physics

and allow the user to select one of the above, storing the url and a modified version of the url in a variable. I'd like to know if I've done this in the best possible way, and also get general feedback.

##Code import re siteslst = {} with open('sites.txt', 'r') as s: data = s.read() data = data.splitlines() for i in data: keyVal = re.search(r'(\w+).',str(i)).group(1) siteslst[str(keyVal)] = str(i) testInput = siteslst.keys() print('Sites: ', ', '.join(testInput), '\n') validInput = False while validInput == False: searchSite = input("which site would you like to search? ") if searchSite in testInput: url = siteslst[searchSite] urlSearch = siteslst[searchSite]+'/search?q=' validInput = True else: validInput = False

Code

import re
siteslst = {}
with open('sites.txt', 'r') as s:
 data = s.read()
 data = data.splitlines()
 for i in data:
 keyVal = re.search(r'(\w+)\.',str(i)).group(1)
 siteslst[str(keyVal)] = str(i)
testInput = siteslst.keys()
print('Sites: ', ', '.join(testInput), '\n')
validInput = False
while validInput == False:
 searchSite = input("which site would you like to search? ")
 if searchSite in testInput:
 url = siteslst[searchSite]
 urlSearch = siteslst[searchSite]+'/search?q='
 validInput = True
 else:
 validInput = False

This code is for part of my first major contribution to open-source, for the socli project. The purpose is to allow the user to select a site from the stack exchange network to browse and fill two variables with the appropriate urls (used through the rest of the program). Since the stack exchange network is adding new sites all the time, I wanted to make it decently easy to update the stored list. Also, I'm lazy, and didn't want to manually create a dictionary =) So I use a text file with the list of sites copy-pasted into it and the code takes care of the rest.

##Approach

The purpose of the code is to go through a text file named sites.txt filled with urls in a format like

https://codereview.stackexchange.com
https://physics.stackexchange.com

Taking the above, it would then produce a dictionary looking like

{'codereview':'https://codereview.stackexchange.com', 'physics':'https://physics.stackexchange.com'}

It would also print

Sites: codereview, physics

and allow the user to select one of the above, storing the url and a modified version of the url in a variable. I'd like to know if I've done this in the best possible way, and also get general feedback.

##Code import re siteslst = {} with open('sites.txt', 'r') as s: data = s.read() data = data.splitlines() for i in data: keyVal = re.search(r'(\w+).',str(i)).group(1) siteslst[str(keyVal)] = str(i) testInput = siteslst.keys() print('Sites: ', ', '.join(testInput), '\n') validInput = False while validInput == False: searchSite = input("which site would you like to search? ") if searchSite in testInput: url = siteslst[searchSite] urlSearch = siteslst[searchSite]+'/search?q=' validInput = True else: validInput = False

This code is for part of my first major contribution to open-source, for the socli project. The purpose is to allow the user to select a site from the stack exchange network to browse and fill two variables with the appropriate urls (used through the rest of the program). Since the stack exchange network is adding new sites all the time, I wanted to make it decently easy to update the stored list. Also, I'm lazy, and didn't want to manually create a dictionary =) So I use a text file with the list of sites copy-pasted into it and the code takes care of the rest.

Approach

The purpose of the code is to go through a text file named sites.txt filled with urls in a format like

https://codereview.stackexchange.com
https://physics.stackexchange.com

Taking the above, it would then produce a dictionary looking like

{'codereview':'https://codereview.stackexchange.com', 'physics':'https://physics.stackexchange.com'}

It would also print

Sites: codereview, physics

and allow the user to select one of the above, storing the url and a modified version of the url in a variable. I'd like to know if I've done this in the best possible way, and also get general feedback.

Code

import re
siteslst = {}
with open('sites.txt', 'r') as s:
 data = s.read()
 data = data.splitlines()
 for i in data:
 keyVal = re.search(r'(\w+)\.',str(i)).group(1)
 siteslst[str(keyVal)] = str(i)
testInput = siteslst.keys()
print('Sites: ', ', '.join(testInput), '\n')
validInput = False
while validInput == False:
 searchSite = input("which site would you like to search? ")
 if searchSite in testInput:
 url = siteslst[searchSite]
 urlSearch = siteslst[searchSite]+'/search?q='
 validInput = True
 else:
 validInput = False
edited tags
Link
alecxe
  • 17.5k
  • 8
  • 52
  • 93
Source Link
auden
  • 441
  • 2
  • 12

Selecting site to browse in python

This code is for part of my first major contribution to open-source, for the socli project. The purpose is to allow the user to select a site from the stack exchange network to browse and fill two variables with the appropriate urls (used through the rest of the program). Since the stack exchange network is adding new sites all the time, I wanted to make it decently easy to update the stored list. Also, I'm lazy, and didn't want to manually create a dictionary =) So I use a text file with the list of sites copy-pasted into it and the code takes care of the rest.

##Approach

The purpose of the code is to go through a text file named sites.txt filled with urls in a format like

https://codereview.stackexchange.com
https://physics.stackexchange.com

Taking the above, it would then produce a dictionary looking like

{'codereview':'https://codereview.stackexchange.com', 'physics':'https://physics.stackexchange.com'}

It would also print

Sites: codereview, physics

and allow the user to select one of the above, storing the url and a modified version of the url in a variable. I'd like to know if I've done this in the best possible way, and also get general feedback.

##Code import re siteslst = {} with open('sites.txt', 'r') as s: data = s.read() data = data.splitlines() for i in data: keyVal = re.search(r'(\w+).',str(i)).group(1) siteslst[str(keyVal)] = str(i) testInput = siteslst.keys() print('Sites: ', ', '.join(testInput), '\n') validInput = False while validInput == False: searchSite = input("which site would you like to search? ") if searchSite in testInput: url = siteslst[searchSite] urlSearch = siteslst[searchSite]+'/search?q=' validInput = True else: validInput = False

lang-py

AltStyle によって変換されたページ (->オリジナル) /