Pulling Vars from lists and applying them to a function
Clash Royale CLAN TAG#URR8PPP
Pulling Vars from lists and applying them to a function
I'm working on a project in my work using purely Python 3:
If I take my scanner, (Because I work in inventory) and anything I scan goes into a text doc, and I scan the location "117" , and then I scan any device in any other location, (the proceeding lines in the text doc "100203") and I run the script and it plugs in '117' in the search on our database and changes each of the devices (whether they were assigned to that location or not) into that location, (Validating those devices are in location '117')
My main question is the 3rd objective down from the Objectives list below that doesn't have "Done" after it.
Objective:
Pull strings from a text document, convert it into a dictionary. = (Text_Dictionary) **Done**
**Done**
Assign the first var in the dictionary to a separate var. = (First_Line) **Done**
**Done**
All proceeding var's greater then the first var in the dictionary should be passed into a function individually. = (Proceeding_Lines)
Side note: The code should loop in a fashion that should (.pop) the var from the dictionary/list, But I'm open for other alternatives. (Not mandatory)
What I already have is:
Project.py:
1 import re
2 import os
3 import time
4 import sys
5
6 with open(r"C:Users...text_dictionary.txt") as f:
7 Text_Dictionary = [line.rstrip('n') for line in
8 open(r"C:Users...text_dictionary.txt")]
9
10 Text_Dict = (Text_Dictionary)
11 First_Line = (Text_Dictionary[0])
12
13 print("The first line is: ", First_Line)
14
15 end = (len(Text_Dictionary) + 1)
16 i = (len(Text_Dictionary))
17
What I have isn't much on the surface, but I have another "*.py
" file fill of code that I am going to copy in for the action that I wish to preform on each of the vars in the Text_Dictionary.txt
. Lines 15 - 16 was me messing with what I thought might solve this.
*.py
Text_Dictionary.txt
In the imported text document, the var's look very close to this (Same length)(All digits):
Text_Dictionary.txt:
117
23000
53455
23454
34534
...
Note: These values will change for each time the code is ran, meaning someone will type/scan in these lines of digits each time.
Explained concept:
Ideally, I would like to have the first line point towards a direction, and the rest of the digits would follow; however, each (Example: '53455'
) needs to be ran separately then the next in line and (Example: '117'
) would be where '53455'
goes. You could say the first line is static throughout the code, unless otherwise changed inText_Dictionary.txt
. '117'
is ran in conjunction with each iteration proceeding it.
'53455'
'117'
'53455'
Text_Dictionary.txt
'117'
Background:
This is for inventory management for my office, I am in no way payed for doing this, but it would make my job a heck-of-a-lot easier. Also, I know basic python to get myself around, but this kinda stumped me. Thank you to whoever answers!
Also, what is your question? "kinda stumped me" is not a problem description. StackOverflow is not a coding, design, or tutorial service.
– Prune
Aug 10 at 16:26
To give more detail, by 'run' one of these vars = variables (int) is: for example:
– Quantum Nebula
Aug 10 at 16:58
Example: If I take my scanner, (Because I work in inventory) and anything I scan goes into a text doc, and I scan the location "117" , and then I scan anything in any other location, (the proceeding lines in the text doc "100203") and I run the script and it plugs in '117' in the search on our database and changes each of the devices (whether they were assigned to that location or not) into that location, (Validating those devices are in location '117') that is what I am trying to obtain, my main question is the 3rd objective down from the top of the post that doesn't have Done after it.
– Quantum Nebula
Aug 10 at 17:10
My apologies, I see how I could be really vague on some of my post. I'll update it with the Example comment above, included.
– Quantum Nebula
Aug 10 at 17:14
1 Answer
1
I've no clue what you're asking, but I'm going to take a guess. Before I do so, your code was annoying me:
with open("file.txt") as f:
product_ids = [line.strip() for line in f if not line.isspace()]
There. That's all you need. It protects against blank lines in the file and weird invisible spaces too, this way. I decided to leave the data as strings because it probably represents an inventory ID, and in the future that might be upgraded to "53455-b.42454#62dkMlwee"
.
"53455-b.42454#62dkMlwee"
I'm going to hazard a guess that you want to run different code depending on the number at the top. If so, you can use a dictionary containing functions. You said that you wanted to run code from another file, so this is another_file.py
:
another_file.py
__all__ = ["dispatch_whatever"]
dispatch_whatever =
def descriptive_name_for_117(product_id):
pass
dispatch_whatever["117"] = descriptive_name_for_117
And back in main_program.py
, which is stored in the same directory:
main_program.py
from another_file import dispatch_whatever
for product_id in product_ids[1:]:
dispatch_whatever[product_ids[0]](product_id)
Yeah, my txt. import function was a bit outdated, This is pretty close to exactly what I was after, the way I described it made it kinda challenging for a lot of people, which is on me. This may work well, I'll comeback to display what I came up with. Totally apologize for the vague post.
– Quantum Nebula
Aug 10 at 17:27
In future, I recommend providing example code for what you want to do, with gaps where you don't know what to put.
– wizzwizz4
Aug 10 at 17:45
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is a "var"? These look merely like integers in a text file. What does it mean to "ran" one of these? You can't "run" a string or an integer. What does it mean for a line to point towards a direction? When you're dealing with a whole line, what are "the rest of the digits"?
– Prune
Aug 10 at 16:25