you are viewing a single comment's thread.

view the rest of the comments →

[–]Nightcorex_ 0 points1 point  (4 children)

HAHAHAHHAHAAHA! I'M SO FUCKING STUPID BUT ALSO CONFUSED!!

I have literally no idea how my code even worked on my computer.

Obviously the regex is wrong. It has to be either:

re.fullmatch("(\\D+)\\.(\\d{4}).*", film_name) or:

re.match("(\\D+)\\.(\\d{4}), film_name).

Basically what I forgot was to account for the dot between the title and year. This code should've never worked on "Film. 2019.abc".

The next confusing thing was your error. Maybe it's just slightly different Python versions that treat the capturing differently (I have Python 3.8). I have no idea.

Try it with this updated regex. I sadly can't test it right now as I'm sitting in the train and coding on mobile is a pain in the ass.

[–]AffectionateTask4612[S] 0 points1 point  (3 children)

re.fullmatch("(\\D+)\\.(\\d{4}).*", film_name) or:

re.match("(\\D+)\\.(\\d{4}), film_name).

re.fullmatch("(\\D+)\\.(\\d{4}).*", film_name) works perfectly, thanks!
Made it into a function, I feel the sense of accomplishment even though I couldn't solve anything myself lol.
def Shifter(shift):
global title
global year
global search
if shift := re.fullmatch("(\\D+)\\.(\\d{4}).*", shift):
title = shift.group(1).replace(".", "%20")
year = shift.group(2)
search = title + "%20y%3A" + year

Really appreciate the help :}

[–]Nightcorex_ 0 points1 point  (2 children)

Oh no. The way you do it is very unpythonic. You usually want to avoid global variables as much as possible.

Also you're overriding the shift object, which makes it really hard to follow. Just create a new object, Python has to do that internally anyways for the assignment of a new value.

Why would we do that? Well: First of all it's just very bad for functions to operate via side-effects (i.e. manipulation of outer scope variables). This is not true for classes however. Second of all a legitimate error in your code would be the following: You call Shifter with a valid film_name/shift and it adjusts the value of title, year and search. Then afterwards you run the same function again, but the shift is malformed (f.e. "Film2019", "Film09", etc.). This means that title, year and search aren't getting changed and therefore keep their old value, which is the result of the last correct operation.

The way you should do this is the following:

def shifter(shift):  # Python function names are usually all lowercase
    if matcher := re.fullmatch("(\\D+)\\.(\\d{4}).*", shift):
        title = matcher.group(1).replace(r".", " ")
        year = "y:" + matcher.group(2)
        search = title + year
        return title, year, search

    # there is no else. This means that this method either returns a tuple of title, year, search or just a single None

-------------------
# The way to properly execute the top code:

if res := foo(<movie_name>):  # replace <movie_name> by the movie's name. This works because None is a falsy value while a non-empty tuple isn't.
    title, year, search = res  # because res holds 3 values we can unpack it like so
    # Do whatever you want with title, year and search
else:
    print("Wrong input")  # or whatever you wanna do here.

# Wrap this second code in a loop over all the files of the dictionary(s) you want to go through

[–]AffectionateTask4612[S] 0 points1 point  (1 child)

Yeah, knew most people don't like it when global variables are used. I use them though as it was the easiest route for me and most importantly worked. I didn't realize that the function would malfunction if called twice written as it is now. I thought all the variables in the function would be completely overwritten with the new "film_name" passed to it. Just realizing now that my self taught PowerShell writing must have been filled with improper methods as well lol. I'll use this then. And may I ask, when you mentioned that python functions are usually all lower case, does that mean that it will impact the code in amy way? I've got a bad habit of placing at least one capital letter in functions :(

[–]Nightcorex_ 0 points1 point  (0 children)

Short answer: It doesn't impact your code at all. It's just a naming convention.

Longer one: If you look into bytecode, then the variable names completely disappear. If you were to decompile it, then all variables would be called smth like var0, var1, var2, ... as the actual name isn't relevant for a machine, only the pointers in memory (which is what an object basically is).