r/zsh • • Mar 20 '22

Is it possible to delete a variable in an array found in a python file using zsh?

I know this isn't practical, but I'm more just curious if this is possible as I am new to zsh aside from basic commands. If it is possible, how would I go about doing it? From what I could find on google it seems like maybe sed would be my best bet?

0 Upvotes

10 comments sorted by

3

u/catwok Mar 20 '22 edited Mar 20 '22

Umm look into sed or awk first. Then you can look into bash/zsh aliases to wrap up your solution with a terminal function.

But also you should make an edit and provide more context to your problem. Chances are we might have another direction for depending on what your problem statement is.

Like why do you need to rm a variable? Do you need to delete the value or the variable itself? And again why? Why do you need to predictably delete the same variable to a python script all the time?

1

u/[deleted] Mar 20 '22

I made a simple PRAW bot for my non programming friend to scan new posts within certain subs for anything having to do with a specific genus of mushroom that he is doing research on. In the event that a subreddit produces poor quality results, I want to be able to send him a terminal command that he can paste into his terminal and remove that subreddit from the array of subreddits its searching (subreddits are stored as strings within an array). I sent the PRAW bot to him with a zsh script that he entered his reddit credentials into which got turned into a python file and pushed into the PRAW bot folder so that way I didn't have to have him give me his password, otherwise I could just manually delete it if it were on my computer. Kind of an odd situation, but I thought that it was a good excuse to learn some more zsh :)

6

u/i_hate_shitposting Mar 20 '22

That seems like a really confusing way of doing things. Why not just modify your Python script to read a config file with his credentials and the subreddits to scan? The built-in json or configparser packages would work fine for that.

Ideally, I would think you could tell him to just open the config file and edit it himself, but if terminal commands are easier, you could use argparse to make a simple CLI with commands to initialize the config file with his credentials and the default list of subreddits as well as add and remove subreddits.

2

u/catwok Mar 20 '22

Yeah I like your way much more. Just have a yaml or json config file of subreddits to include and tell them to use vscode to edit it.

1

u/[deleted] Mar 20 '22

He doesn't have vscode and I don't want him to have to download it just for this. He isn't a computer savvy person he just happens to use Reddit (which surprised me in an of itself) so I'd like to just be able to send him a terminal command. I know this isn't ideal, but is it possible?

4

u/ProsecutedMeatloaf Mar 20 '22

Any text editor would work. And honestly it probably could just be a flat file. Nothing i have read indicates it meds to be yaml or json.

If you really want to do this in zsh/bash, then it’s pretty easy to wrap what you want to do in functions

Eg

remove-sub () {
   sub="${1}"
   sed "s/${sub}\n//g" path/to/config/file
}
add-sub () {
   sub="${1}"
   echo "${sub}" >> path/to/config/file
}

The same can be done to edit the python file directly but that’s much more prone to errors so checks would need to be put in place to make sure its valid python or else your script could break. Reading from a file is much cleaner

1

u/[deleted] Mar 20 '22

Thats what the file does. It gets his username, password, and refresh token and generates a python file that the main file imports those variables from the python file with his information on it for PRAW to read and use for authentication. If that specific way of going about it isn't optimal/ideal that's probably because I suck at programming haha. Would I have to build a CLI though? I was hoping I could just send him a terminal command to use where he could switch out a variable within the command with the specific sub he wants to remove. Something like:

cd PRAWbot; from credentials.py remove "SUBREDDITNAME"

Is something this simple easily doable in zsh? Sorry if this a stupid question, I'm still new to zsh.

Edit: I also want a kind of quick and dirty approach like I outlined above because this is all in the event he even wants to remove a sub from the list of subs being scanned. The most likely outcome is that he'll just ignore a sub if it doesn't produce much. I just wanted to have something like this ready for him in the unlikely event he would like to remove a sub.

3

u/catwok Mar 20 '22 edited Mar 20 '22

This is not a sustainable method imo. Its too many 'jumping jacks' so to speak, for the work that is actually needed to be performed. u/i_hate_shitposting has a good direction for you I think -- have them edit the config file of subreddits themselves.

Its typically preferable or at least worthwhile, to look for social and behavorial solutions to some forms of problems versus programming for them.

Let your software focus on the 'work being performed' and you can always circle back later for these little nicetys.

Spend time on core functionality. You could add syntax check for the config file and warn if its insane -- this would help your friend correct the issue themself versus calling you to work on it.

This is not a great example since the library may already provide sufficient explanation -- but this sort of idea is what I mean to focus on here.

2

u/Spellbinder32 Mar 20 '22

just make the script take command line argumens? or read from a .txt file containing list of subreddits?

1

u/i_hate_shitposting Mar 20 '22 edited Mar 20 '22

I mean, yes, technically what you describe is possible, but it’s just… not how you do things. I don’t blame you at all for asking about or trying this approach, but I’ve been programming for about 20 years now and it’s with that experience that I can say this isn’t an approach I would ever recommend.

You mentioned you want to do this just in case he asks, but he hasn’t yet, which sounds to me like you want practice. I would argue that’s that much more reason to try the proven way. :)

The json package and argparse package are both pretty straightforward and built into the language, and they’re both super useful to learn. If you can use PRAW I’m sure you can get it pretty quickly.

And if you want quick and dirty, you could easily break it into a few smaller chunks. To start with, you could use the input function to get inputs instead of argparse. You could start by changing your PRAW bot to read the username, password, and subreddits from a file and write the json yourself. Then write a script that asks for the username and password and generates the json file with the creds and the list of subreddits. Then write another to prompt for a subreddit to remove from the list and you’ve got a basic first pass done.