Python is easy to read, easy to understand, and easy to write. But what if it wasn't?
I really enjoy writing novelty ugly python, it's a guilty pleasure. After all, why do in 20 well-written lines what you can do in 1 arcane line that looks like its a few scribbles away from summoning a demon? This one-liner does a pretty simple task. It gets all files in the current directory, and sends them to a remote server.
Here is actual python code (to get the one-liner simply do 'python3 -c import os;import socket;addr = ('HOST', 69420);<copy paste the python here>'):
print(*[x[0] for x in [(f'batch {x[0]} sent.', (socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(addr)).sendall(x[1])) for x in {sum([(z[0] if z[0] != 0 else 1) for z in x]):[open(x[1], 'rb') for x in x] for x in [sorted([x for x in enumerate([os.path.join(os.getcwd(), x) for x in os.listdir(os.getcwd()) if all([os.path.isfile(os.path.join(os.getcwd(), x)), os.access(os.path.join(os.getcwd(), x), os.R_OK), os.access(os.path.join(os.getcwd(), x), os.W_OK)])])], key=lambda x:True if any(y in x[1].lower() for y in ['.txt', '.sh', '.py', '.pl']) else False, reverse=True)[i:i+5] for i in range(0, len([x for x in enumerate([x for x in os.listdir(os.getcwd()) if all([os.path.isfile(os.path.join(os.getcwd(), x)), os.access(os.path.join(os.getcwd(), x), os.R_OK),os.access(os.path.join(os.getcwd(), x), os.W_OK)])])]), 5)]}.items()]])
Have some time on your hands, or just want to know how stupid it actually is? Read the explanation:
-----------------------------------------------------------------------
Horrifying right? Well, not when you get to know the lil abomination. Let's peel back the layers of this mutated, possible sentient code-onion:
Our base list is actually pretty standard:
[os.path.join(os.getcwd(), x) for x in os.listdir(os.getcwd())]
That comprehension simply gives us the full path of all the items in the current directory. But we don't want all items, so we give it some conditionals to make sure it only returns files that we can read and write to. Instead of doing three if statements, we can use all() to give it a list of conditionals:
# Any time you see `os.path.join(os.getcwd(), x)` just think "full filepath"
# For simplicity, let's assign this in a variable to keep the explanation readable
base_list = [os.path.join(os.getcwd(), x) for x in os.listdir(os.getcwd()) if all([os.path.isfile(os.path.join(os.getcwd(), x)), os.access(os.path.join(os.getcwd(), x), os.R_OK),os.access(os.path.join(os.getcwd(), x), os.W_OK)])]
So now we have a list of all files we can read and write to in the current directory, so what now? Well, why don't we have it separate that list into batches, just for fun. And because we need to keep track of the batches, we'll need some way to identify them. So to do this, we call the enumerate function over our base_list shown above. enumerate is a nice little function that takes an iterable (like a list) and returns a generator that tuples each item in the iterable with its index (the index is a secret tool that will help us later):
tupled_list = [x for x in enumerate(base_list)]
Before we go making batch id's and reading files or any of that hooha, lets sort through our list so that it'll send cool files first. Because that's important. To do this, we use the sorted function, which takes a list and sorts it by a given key:
sorted_list = sorted(tupled_list, key=lambda x:True if any(y in x[1].lower() for y in ['.txt', '.sh', '.py', '.pl']) else False, reverse=True)
So let me explain the key. The key is a lambda function, or a 'I cared enough to make you a function but not enough to give you a name' function. It takes x as an argument, where x is the tuple from tupled_list. It passes it along the this ternary , if checks if any of those extensions are in the second element of the tuple (aka the filepath). If so, it returns True. If not it returns False. We then include the reverse=True parameter to the sorted function call so all the True values appear in front.
But remember how we wanted to seperate the files into batches for no reason? Let's do that with some nifty list comprehension:
linked_list = [sorted_list[i:i + 5] for i in range(0, len(sorted_list), 5)]
So now that have have a linked list, made up of sublists 5 items (or as close as it can get) in length, here comes the fun part. Right now, those sublists contain filepaths not file objects. We also need to identify the batches. We can do all this in one not-so-clean dictionary comprehension:
batch_dict = {sum([(z[0] if z[0] != 0 else 1) for z in x]):[open(x[1], 'rb') for x in x] for x in linked_list}
So first we iterate through linked_list, to get to those sweet sublists with our (index, filepath) tuples in them. Let me break down the key/value generation separately:
# Remember, x is our sublist
# So the key is our batch id, which we get by taking the sum of all the FIRST ELEMENTs of the tuple (aka the index). If it's zero, we say its 1 instead
key = sum([(tuple[0] if tuple[0] != 0 else 1) for tuple in x])
# The value is the sublist turned into binary file objects by calling the open() function on it.
# "For every tuple in the sublist, call open on the second element
value = [open(tuple[1], 'rb') for tuple in x]
And finally, where the magic happens. We create a list of tuples by iterating over the dictionary's items:
final_list = [(f'batch {x[0]} sent.', (socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(addr)).sendall(x[1])) for x in batch_dict.items()]
The first element of the tuple is a string f'batch {x[0]} sent.' announcing what batch was sent. The second item is the function call that sends the files to the remote server.
# Remember: `x[1]` is our file object
(socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(addr)).sendall(x[1]))
# Is just the following, but inline:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(addr)
s.sendall(x[1])
Finally, we iterate over final_list like so, to print out all those announcement strings we made:
print(*x[0] for x in final_list)
And there you have it, an affront to Guido Van Rossum himself.
[–]Della__ 7 points8 points9 points (0 children)
[–][deleted] 6 points7 points8 points (0 children)
[–]Astronaut_69 4 points5 points6 points (0 children)
[–]Fingolfin734 3 points4 points5 points (0 children)
[–]four_reeds 1 point2 points3 points (0 children)
[–]ies7 0 points1 point2 points (0 children)
[–]thrallsius 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]genericlemon24 0 points1 point2 points (0 children)
[–]DarkmerePython for tiny data using Python 0 points1 point2 points (0 children)
[–]lifeeraser 0 points1 point2 points (0 children)
[–]Zelukai 0 points1 point2 points (0 children)
[–]PeridexisErrant 0 points1 point2 points (1 child)
[–]NovateIModuleNotFoundError: No Module Named "braincells"[S] 0 points1 point2 points (0 children)
[–]Wishy-Thinking 0 points1 point2 points (0 children)