Editing and Capturing new option from optionMenu Tkinter - for-loop

I created a list of optionMenus via a for loop. I need to tie my optionMenu selection to the optionMenu it came from. The for loop, I think, is making it so I am unable to tie the two values together.
def get_selection(choice):
popUp_list.append(choice)
if num >= 1 and num <5:
for num in range(1,5):
choice = StringVar(root2)
choices = {'Application', 'File', 'Website'}
choice.set('Choose Type')
popUpMenu = OptionMenu(root2, choice, *choices, command = Controller.get_selection)
popUpMenu.grid(row=num, column=0)
I know it would be easier to hard code the option menus, but for reasons I don't want to get in to, the for loop is necessary. Not for this part specifically, but for another part of my code. It's just easier to use this example here.
When an option is selected from the menus, I am able to pull the values and that works great. However, if a user makes a first selection and then wants to update their selection, I have no way to capture that. It creates a "new" selection, not tied to the optionMenu. So the new option does not replace the old option. It just creates a new selection.
Is there a way to pass the popUpMenu number through the Controller .get_selection function? When trying to pass another variable doing something on the lines of:
def get_selection(choice, num):
popUp_list.append(choice)
print(num)
popUpMenu = OptionMenu(root2, choice, *choices, command = lambda: Controller.get_selection(choice, num))
I get the error below and I'm not able to get the option selection. I'm not sure what to put as the first value. Inputting choice does not work.
self.__callback(self.__value, *args)
TypeError: () takes 0 positional arguments but 1 was given

I was able to solve this. I updated my code to :
if num >= 1 and num <5:
for num in range(1,5):
choice = StringVar(root2)
choices = {'Application', 'File', 'Website'}
choice.set('Choose Type')
popUpMenu = OptionMenu(root2, choice, *choices, command = lambda choice = choice, num = num: (Controller.get_selection(choice, num)))
So, making your command statement like:
command = lambda choice = choice, num = num: (Controller.get_selection(choice, num)))
allows you to pass variables into your function.

Related

Python: Printing vertically

The final code will print the distance between states. I'm trying to print the menu with the names of the states numbered and vertically. I really struggle to find my mistakes.
This code doesn't raise any error, it just prints nothing, empty.
state_data = """
LA 34.0522°N 118.2437°W
Florida 27.6648°N 81.5158°W
NY 40.7128°N 74.0060°W"""
states = []
import re
state_data1 = re.sub("[°N#°E]", "", state_data)
def process_states(string):
states_temp = string.split()
states = [(states_temp[x], float(states_temp[x + 1]), float(states_temp[x + 2])) for x in
range(0, len(states_temp), 3)]
return states
def menu():
for state_data in range(state_data1):
print(f'{state_data + 1} {name[number]}')
My first guess is, your code does not print anything without errors because you never actually execute process_airports() nor menu().
You have to call them like this at the end of your script:
something = process_airports(airport_data1)
menu()
This will now raise some errors though. So let's address them.
The menu() function will raise an error because neither name nor number are defined and because you are trying to apply the range function over a string (airport_data1) instead of an integer.
First fixing the range error: you mixed two ideas in your for-loop: iterating over the elements in your list airport_data1 and iterating over the indexes of the elements in the list.
You have to choose one (we'll see later that you can do both at once), in this example, I choose to iterate over the indexes of the list.
Then, since neither name nor number exists anywhere they will raise an error. You always need to declare variables somewhere, however, in this case they are not needed at all so let's just remove them:
def menu(data):
for i in range(len(data)):
print(f'{i + 1} {data[i]}')
processed_airports = process_airports(airport_data1)
menu(processed_airports)
Considering data is the output of process_airports()
Now for some general advices and improvements.
First, global variables.
Notice how you can access airport_data1 within the menu() function just fine, while it works this is not something recommended, it's usually better to explicitly pass variables as arguments.
Notice how in the function I proposed above, every single variable is declared in the function itself, there is no information coming from a higher scope. Again, this is not mandatory but makes the code way easier to work with and understand.
airport_data = """
Alexandroupoli 40.855869°N 25.956264°E
Athens 37.936389°N 23.947222°E
Chania 35.531667°N 24.149722°E
Chios 38.343056°N 26.140556°E
Corfu 39.601944°N 19.911667°E"""
airports = []
import re
airport_data1 = re.sub("[°N#°E]", "", airport_data)
def process_airports(string):
airports_temp = string.split()
airports = [(airports_temp[x], float(airports_temp[x + 1]), float(airports_temp[x + 2])) for x in
range(0, len(airports_temp), 3)]
return airports
def menu(data):
for i in range(len(data)):
print(f'{i + 1} {data[i]}')
# I'm adding the call to the functions for clarity
data = process_airports(airport_data1)
menu(data)
The printed menu now looks like that:
1 ('Alexandroupoli', 40.855869, 25.956264)
2 ('Athens', 37.936389, 23.947222)
3 ('Chania', 35.531667, 24.149722)
4 ('Chios', 38.343056, 26.140556)
5 ('Corfu', 39.601944, 19.911667)
Second and this is mostly fyi, but you can access both the index of a iterable and the element itself by looping over enumerate() meaning, the following function will print the exact same thing as the one with range(len(data)). This is handy if you need to work with both the element itself and it's index.
def menu(data):
for the_index, the_element in enumerate(data):
print(f'{the_index + 1} {the_element}')

Python 2, raw_input in define function. Returning value

I decided to have a little bit fun with coding python 2 in a long while and decided to try make somewhat playable game of Go.
I quickly tumbled on this issue while trying to make function for the game, it seems like I'm not understanding how raw_input or functions work fundamentally. I cut the part of the code that is giving me the trouble here. When I try to run this, I can get to give inputs, but after that I get NameError: name 'crd_x' is not defined, in the stoneplacement line. The code does work without using the function, but when I try to clean it up like this, I get the said error.
What exactly does return crd give out and how I'm actually supposed to give out variables from def functions?
def checkplayerinput():
if player_on_turn == 0:
crd = raw_input("Place Black stone (X-Y): ").split("-")
elif player_on_turn == 1:
crd = raw_input("Place White stone (X-Y): ").split("-")
crd_x = int(crd[1])
crd_y = int(crd[0])
return crd_x, crd_y
def stoneplacement(crd_x, crd_y, player_on_turn):
if board[crd_x][crd_y] == "+" and player_on_turn == 0:
board[crd_x][crd_y] = "B"
elif board[crd_x][crd_y] == "+" and player_on_turn == 1:
board[crd_x][crd_y] = "W"
stop = 0
while stop == 0:
#User input and derive coordinates
checkplayerinput()
# Stone placement
stoneplacement(crd_x, crd_y, player_on_turn)
edit. just switched the place of crd_x and crd_y like they are in the actual code. It produces exact same NameError however.
So crd is going to give you an array of strings based on whatever the user types in, separated by the ‘-‘ symbols. For example if player_on_turn == 0, and you entered for example “X-Y” (whatever your game calls for), checkplayerinput() would return crd as [“X”, “Y”].
Also, remember that an array is indexed starting at 0. So in this example, crd[0] == “X”.
Running through your code, does that explain why your stoneplacement() function throws a NameError? I.e. are you inputting the right raw_input to yield crd[1]?
Also, did you define crd before def checkplayerinput() in the code? According to python scope rules, if you define the variable crd inside of the function, it will not have a global value outside of it (and will throw an error if you reference it in another function). This is likely why you are able to run your code without the function definitions. Try adding a line “global crd” or “crd = [ ]” somewhere outside of the function and it will allow you to access crd in the stoneplacement(( function.

Graphlab: How to avoid manually duplicating functions that has only a different string variable?

I imported my dataset with SFrame:
products = graphlab.SFrame('amazon_baby.gl')
products['word_count'] = graphlab.text_analytics.count_words(products['review'])
I would like to do sentiment analysis on a set of words shown below:
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
Then I would like to create a new column for each of the selected words in the products matrix and the entry is the number of times such word occurs, so I created a function for the word "awesome":
def awesome_count(word_count):
if 'awesome' in product:
return product['awesome']
else:
return 0;
products['awesome'] = products['word_count'].apply(awesome_count)
so far so good, but I need to manually create other functions for each of the selected words in this way, e.g., great_count, etc. How to avoid this manual effort and write cleaner code?
I think the SFrame.unpack command should do the trick. In fact, the limit parameter will accept your list of selected words and keep only these results, so that part is greatly simplified.
I don't know precisely what's in your reviews data, so I made a toy example:
# Create the data and convert to bag-of-words.
import graphlab
products = graphlab.SFrame({'review':['this book is awesome',
'I hate this book']})
products['word_count'] = \
graphlab.text_analytics.count_words(products['review'])
# Unpack the bag-of-words into separate columns.
selected_words = ['awesome', 'hate']
products2 = products.unpack('word_count', limit=selected_words)
# Fill in zeros for the missing values.
for word in selected_words:
col_name = 'word_count.{}'.format(word)
products2[col_name] = products2[col_name].fillna(value=0)
I also can't help but point out that GraphLab Create does have its own sentiment analysis toolkit, which could be worth checking out.
I actually find out an easier way do do this:
def wordCount_select(wc,selectedWord):
if selectedWord in wc:
return wc[selectedWord]
else:
return 0
for word in selected_words:
products[word] = products['word_count'].apply(lambda wc: wordCount_select(wc, word))

Returning multiple ints and passing them as multiple arguements in Lua

I have a function that takes a variable amount of ints as arguments.
thisFunction(1,1,1,2,2,2,2,3,4,4,7,4,2)
this function was given in a framework and I'd rather not change the code of the function or the .lua it is from. So I want a function that repeats a number for me a certain amount of times so this is less repetitive. Something that could work like this and achieve what was done above
thisFunction(repeatNum(1,3),repeatNum(2,4),3,repeatNum(4,2),7,4,2)
is this possible in Lua? I'm even comfortable with something like this:
thisFunction(repeatNum(1,3,2,4,3,1,4,2,7,1,4,1,2,1))
I think you're stuck with something along the lines of your second proposed solution, i.e.
thisFunction(repeatNum(1,3,2,4,3,1,4,2,7,1,4,1,2,1))
because if you use a function that returns multiple values in the middle of a list, it's adjusted so that it only returns one value. However, at the end of a list, the function does not have its return values adjusted.
You can code repeatNum as follows. It's not optimized and there's no error-checking. This works in Lua 5.1. If you're using 5.2, you'll need to make adjustments.
function repeatNum(...)
local results = {}
local n = #{...}
for i = 1,n,2 do
local val = select(i, ...)
local reps = select(i+1, ...)
for j = 1,reps do
table.insert(results, val)
end
end
return unpack(results)
end
I don't have 5.2 installed on this computer, but I believe the only change you need is to replace unpack with table.unpack.
I realise this question has been answered, but I wondered from a readability point of view if using tables to mark the repeats would be clearer, of course it's probably far less efficient.
function repeatnum(...)
local i = 0
local t = {...}
local tblO = {}
for j,v in ipairs(t) do
if type(v) == 'table' then
for k = 1,v[2] do
i = i + 1
tblO[i] = v[1]
end
else
i = i + 1
tblO[i] = v
end
end
return unpack(tblO)
end
print(repeatnum({1,3},{2,4},3,{4,2},7,4,2))

What is the pythonic way to print values right aligned?

I've a list of strings which I want to group by their suffix and then print the values right-aligned, padding the left side with spaces.
What is the pythonic way to do that?
My current code is:
def find_pos(needle, haystack):
for i, v in enumerate(haystack):
if str(needle).endswith(v):
return i
return -1
# Show only Error and Warning things
search_terms = "Error", "Warning"
errors_list = filter(lambda item: str(item).endswith(search_terms), dir(__builtins__))
# alphabetical sort
errors_list.sort()
# Sort the list so Errors come before Warnings
errors_list.sort(lambda x, y: find_pos(x, search_terms) - find_pos(y, search_terms))
# Format for right-aligning the string
size = str(len(max(errors_list, key=len)))
fmt = "{:>" + size + "s}"
for item in errors_list:
print fmt.format(item)
An alternative I had in mind was:
size = len(max(errors_list, key=len))
for item in errors_list:
print str.rjust(item, size)
I'm still learning Python, so other suggestions about improving the code is welcome too.
Very close.
fmt = "{:>{size}s}"
for item in errors_list:
print fmt.format(item, size=size)
The two sorting steps can be combined into one:
errors_list.sort(key=lambda x: (x, find_pos(x, search_terms)))
Generally, using the key parameter is preferred over using cmp. Documentation on sorting
If you are interested in the length anyway, using the key parameter to max() is a bit pointless. I'd go for
width = max(map(len, errors_list))
Since the length does not change inside the loop, I'd prepare the format string only once:
right_align = ">{}".format(width)
Inside the loop, you can now do with the free format() function (i.e. not the str method, but the built-in function):
for item in errors_list:
print format(item, right_align)
str.rjust(item, size) is usually and preferrably written as item.rjust(size).
You might want to look here, which describes how to right-justify using str.rjust and using print formatting.

Resources