|
Mycroft Holmes posted:I've run into a problem with my homework. I have to take the following list: There are some *very* compact ways to do this in python that it sounds like you haven't met yet (and the question hint is suggesting you not use). As this is a homework question I'll just give a couple of hints for now. To populate book_data you'll need to iterate over each entry the current list, transform it into the specified format, then append that transformed value to book_data To do the transformation you should look at either f-strings (if you've met them), or the .join() method. Edit: okay, from your answers above sounds like we need to take a step back. Look at the first element of the list you've been given. What type of object is it? How would you get the title or author from that object? DoctorTristan fucked around with this message at 19:52 on Feb 24, 2022 |
# ? Feb 24, 2022 19:49 |
|
|
# ? May 14, 2024 21:22 |
|
okay, so so the data types are dictionaries. I think I know how to do this then.
|
# ? Feb 24, 2022 19:59 |
|
What is the most concise way in this garbage language? I got this, but my head hurts:Python code:
Ruby code:
|
# ? Feb 24, 2022 20:41 |
|
I'm using Python to tear through some data files, and un-gzipping them in Python is pretty slow. What's a sane pattern for quickly un-gzipping multiple large files to get them into a single Python process? I've read http://www.dalkescientific.com/writings/diary/archive/2020/09/16/faster_gzip_reading_in_python.html, and it looks like one faster approach to ungzipping in Python is to use https://github.com/pycompression/xopen, which launches an external native gzip or pigz, directs its output to a pipe, and reads from the pipe in Python, both getting the better native performance and built-in multiprocessing because now the unzip is in a separate process. Does anybody have sane ideas for how I could set this up to uncompress multiple files at once and get them all into Python in an organized way? I'm using to being able to do simultaneous multi-threading in Java or C# and can't figure out a Pythonic pattern here.
|
# ? Feb 24, 2022 20:49 |
|
Twerk from Home posted:I'm using Python to tear through some data files, and un-gzipping them in Python is pretty slow. What's a sane pattern for quickly un-gzipping multiple large files to get them into a single Python process? Use multiprocessing's Pool to kick it off once per file, then wait for all the jobs in the pool to finish?
|
# ? Feb 24, 2022 21:02 |
|
Electoral Surgery posted:Use multiprocessing's Pool to kick it off once per file, then wait for all the jobs in the pool to finish? My understanding is that it'd be slow-ish to copy data back from a process, especially if it's potentially enormous. I'm assuming you're saying decompress each file in a separate process, then bring it all back to the master process. I need all of the data from all of the files in one python process to be able to re-assemble it. That's a good very basic improvement that I had missed though!
|
# ? Feb 24, 2022 21:04 |
|
This sounds like a problem I might try using asyncio to solve, assuming IO reading these files is substantial
|
# ? Feb 24, 2022 22:52 |
|
Reporting back to say that for my specific usage case, xopen is a huge winner: https://github.com/pycompression/xopen Creates a new process to decompress each compressed file. Uses the Intel gzip decompressor, the fastest in the land. Unless you have a non-Intel CPU. Got a change to allow bigger pipes merged back into CPython to make their specific usage case even faster: https://github.com/python/cpython/pull/21921 This is absolutely perfect for having 5 60GB gzips open at once!
|
# ? Feb 24, 2022 23:38 |
|
Hey gang! I'm struggling through another bit of code and am hoping I could get some insight. The assignment requires me to count the number of words in a string and print it out without using any imported modules. I'm supposed to get the values through loops and a dictionary, perhaps. The string is code:
code:
The first thing I did was get a list of unique words. But after that, I'm a little lost. Without using count, what is the simplest approach to getting these values? A dictionary? code:
|
# ? Feb 25, 2022 22:23 |
|
Use a dictionary. If the word is not in the dictionary, add it, mapping to the value 1. If it is in the dictionary then just increment the value
|
# ? Feb 25, 2022 22:29 |
|
A dictionary is the right approach. Words as keys and the count as the corresponding value. Break it down into the simplest steps. You see a word. Is it in the dictionary? What do you do if it's in the dictionary? What do you do if you haven't seen the word before?
|
# ? Feb 25, 2022 22:29 |
|
another data structure you might find relevant to this assignment is the set e: actually, this is quite wrong, but I'm leaving it up for your reference anyway.
|
# ? Feb 25, 2022 23:48 |
|
Biffmotron posted:A dictionary is the right approach. Words as keys and the count as the corresponding value. I appreciate this. This is one of the problems I'm having most with this course; it's all online and through self-study and, um, autodidactism if that's a word; I try and translate the code into English the same way I translate Spanish grammar into English grammar before I fully comprehend it. And when I'm staring at just a bunch of words that I know mean something but don't automatically translate in a clear way, I get really lost. I suppose eventually I'll acclimate to the code better but in the meantime, yeah, I am staring at the code and I'm staring at the problem and they seem to be in two different languages and I can't bridge them. Soanyway, here is my new code: code:
|
# ? Feb 25, 2022 23:57 |
|
Don't forget f-strings! str.format() is out of date if not quite obsolete. I honestly prefer them because everything is inline.
|
# ? Feb 26, 2022 00:01 |
|
So the assignment actually included this bit about reading from a .csv file, and I ran into a problem that I solved in what I think is a pretty inelegant way:code:
The wordlist string always had a newline thing at the end. code:
edit: f-strings! credburn fucked around with this message at 00:23 on Feb 26, 2022 |
# ? Feb 26, 2022 00:17 |
|
ExcessBLarg! posted:
you dont need the [ ] in the inner comprehension at the very least
|
# ? Feb 26, 2022 00:42 |
|
credburn posted:So the assignment actually included this bit about reading from a .csv file, and I ran into a problem that I solved in what I think is a pretty inelegant way: What you’re looking for is the code:
code:
Example code:
lazerwolf fucked around with this message at 02:41 on Feb 26, 2022 |
# ? Feb 26, 2022 02:31 |
|
credburn posted:I appreciate this. This is one of the problems I'm having most with this course; it's all online and through self-study and, um, autodidactism if that's a word; I try and translate the code into English the same way I translate Spanish grammar into English grammar before I fully comprehend it. And when I'm staring at just a bunch of words that I know mean something but don't automatically translate in a clear way, I get really lost. I suppose eventually I'll acclimate to the code better but in the meantime, yeah, I am staring at the code and I'm staring at the problem and they seem to be in two different languages and I can't bridge them. Tip: you can iterate over keys and values in a dict simultaneously! Also yeah use f-strings they're great, you can basically directly insert variables into strings by just prefixing a string with the letter f: Python code:
credburn posted:So the assignment actually included this bit about reading from a .csv file, and I ran into a problem that I solved in what I think is a pretty inelegant way: You can use strip() to remove leading and trailing newline characters from a string, instead of using replace; replace works too but this is what strip is made for. You can use open() as a context manager, this means that the file is closed even if an error occurs (making context managers extremely useful). code:
|
# ? Feb 26, 2022 05:23 |
Python, where telling a newbie to "use a dictionary" isn't a burn
|
|
# ? Feb 26, 2022 05:46 |
|
https://www.youtube.com/watch?v=-AZUIL1rY3U I'm trying to figure out a way to do python debugging using vimspector in neovim. I watched the video above about setting up vimspector to do debugging, but he only shows how to do this using javascript testing library Jest. I got his version working for javascript just from copy and paste but I'm completely lost on what's actually going on in the code and how I would write something like the JestStrategy function but python instead. I'm guessing it has something to do with `cmd` but I've tried to log out its contents without much success. I don't know how he's coming up with the `testName` if I can't see what's in cmd, I feel like this would get me a bit further along. Sleepy Robot fucked around with this message at 00:04 on Feb 28, 2022 |
# ? Feb 27, 2022 20:05 |
|
Sleepy Robot posted:https://www.youtube.com/watch?v=-AZUIL1rY3U He's doing unit testing with Jest, which is a javascript testing framework. This is nothing really to do with vimspector setup, he's just setting up macros and vim functions to call jest to debug unit tests in the second half of the video - he is all set up with vimspector itself by halfway through the video. There are some guides for setting up vimspector for python on the vimspector site https://github.com/puremourning/vimspector#python which are relatively straightforward, at least as far as vim goes.
|
# ? Feb 28, 2022 02:07 |
|
So I can find guides about how to integrate Black into your coding and precommit hooks. But are there any packages out there that let you run Black on pre-commit and stage the formatted files? Or would I have to write something like that myself? This totally exists on the JavaScript ecosystem so I'm sure there's got to be something out there
|
# ? Feb 28, 2022 05:25 |
|
teen phone cutie posted:So I can find guides about how to integrate Black into your coding and precommit hooks. But are there any packages out there that let you run Black on pre-commit and stage the formatted files? Or would I have to write something like that myself? I use pre-commit to set up black flake8 and isort. Simple yaml file to set everything up.
|
# ? Feb 28, 2022 14:14 |
|
On that topic, is there a way to exclude the venv from a pre-commit processing? I only want to process poo poo that I've written, I don't want to black every loving .py file in my ./env volder. I want to set up a pre-commit so I don't have to manually black everything but last time left a sour taste in my mouth. After an hour sitting there waiting for black to get a quarter of the way through Python itself.
|
# ? Feb 28, 2022 14:51 |
|
I have my editor set up to run black on write if the open file ends in .py.
|
# ? Feb 28, 2022 17:00 |
|
D34THROW posted:On that topic, is there a way to exclude the venv from a pre-commit processing? I only want to process poo poo that I've written, I don't want to black every loving .py file in my ./env volder. I want to set up a pre-commit so I don't have to manually black everything but last time left a sour taste in my mouth. Your venv shouldn't be mingling with your project (e.g. You shouldn't have a ./env folder)
|
# ? Feb 28, 2022 17:16 |
|
This is the first semi-successful venv I've used. It's (mistakenly, admittedly) set up so that the env top level folder is in the project parent folder, same as the main .py files. Next time I'd do it differently. I didn't realize until roughly last night that I did it a Bad Way.
|
# ? Feb 28, 2022 17:43 |
Hrm. Putting the venv in the main project dir is bad? drat, PyCharm uses that as the default location for its integrated virtualenv behavior. Didn't know that was frowned-on.
|
|
# ? Feb 28, 2022 17:48 |
|
Data Graham posted:Hrm. Putting the venv in the main project dir is bad? drat, PyCharm uses that as the default location for its integrated virtualenv behavior. Didn't know that was frowned-on. Not bad per se, but you do run into problems like the OP is experiencing (having to spend time figuring out how to get linters and autoformatters to ignore your env directory when otherwise *.py would do). I tend to use conda environments, those are installed in a common location (somewhere adjacent to your conda/mamba executables)
|
# ? Feb 28, 2022 17:58 |
Yeah I mean, I'm used to excluding it with a .gitignore and so on, but everywhere that I've seen that has black or coverage or other pre-commit stuff integrated also evidently has ways to exclude things. I'd think that would be a basic feature of any such tool, after all venv isn't the only thing you're going to want to exclude in the project dir.
|
|
# ? Feb 28, 2022 18:02 |
|
In your pyproject.toml file add your venv folder to extend-exclude sectioncode:
|
# ? Feb 28, 2022 18:40 |
|
I have a class Order which acts as a container for multiple objects of class Opening. I want Order to be able to take multiple types of input, for instance, a CSV file, a tab-delimited text file, and direct manual input that is passed as a dict by the Flask app. At some point, I may also add an XML file input. For each of these data type inputs, I want to perform a different __init__(), essentially. They will all contain the same basic data, but the structure will differ; the __init__() will also write to the same basic list-of-dicts, each dict of which will be passed to an Opening object in its constructor. The constructor then handles determining which kind of opening it is and processes it accordingly with the appropriate child class. A sample of data, for instance. Here's a row from the CSV and a comparable row from the tab-delimited text format. code:
I sort of understand multiple dispatch, but I'm having a hard time with the @classmethod decorator, or @singledispatchmethod. I suppose I could just take an any input and do an if-elif in the __init__() method? Is that the simplest way to do it? Basically I want to achieve the following: code:
D34THROW fucked around with this message at 19:04 on Mar 2, 2022 |
# ? Mar 2, 2022 18:59 |
|
Yes I think an if tree is a good approach, you can use dictionary dispatch if you want to be slick:code:
|
# ? Mar 2, 2022 19:26 |
|
If I'm interpreting that properly, that's storing a function as a dictionary value, then passing the input as an argument to the function called by accessing the dict key? That is a loving awesome piece of programming and the easiest to syntactically parse for me, thank you! EDIT: I can do it with a list too. If I pass the data type as an int via enum, then the data itself as an any type argument...processors[input_type](raw_data). Python never ceases to amaze. List comprehension blew my mind, this has me D34THROW fucked around with this message at 20:19 on Mar 2, 2022 |
# ? Mar 2, 2022 20:07 |
|
Documentation sure adds a lot of loving lines of codePython code:
Python code:
|
# ? Mar 9, 2022 22:01 |
|
Yeah comments should actually be pretty uncommon imo, the names of functions and variables should make clear what is happening. Also, don't use the names of builtins for variable names: id and type are verboten. The IDE you use should identify this
|
# ? Mar 9, 2022 22:42 |
|
QuarkJets posted:Yeah comments should actually be pretty uncommon imo, the names of functions and variables should make clear what is happening. In the hierarchy of best practices, over-commenting is still a lesser evil than a lot of other options though. I do generally agree though with most of those being superfluous. timestamp is pretty clear (or created_time if you want to be more explicit), and I would only add comments on anything that's weird enough that you can't easily represent it with a nicely specific variable name. Edit: Also, I think that function/method docstrings are way more useful in terms of documentation than individual variable comments. Falcon2001 fucked around with this message at 23:25 on Mar 9, 2022 |
# ? Mar 9, 2022 22:54 |
|
Don't be afraid to use id as a suffix. user_id, report_type_id, job_id, etc. If it's actually an identifier, which most of those variables are, then label it as such. And if it's the primary key then primary_id or row_id are great labels If I see "create_user" then I am going to assume that's a string containing a username or maybe even something else, not necessarily an integer that's actually a foreign key (you are using foreign keys to create relationships between tables, right?)
|
# ? Mar 9, 2022 23:26 |
|
i've always done the object first, then the descriptors. so like, idCustomer, idTransaction, nameFirst, nameLast, etc. helps me understand things a bit quicker in a glance
|
# ? Mar 9, 2022 23:53 |
|
|
# ? May 14, 2024 21:22 |
|
abelwingnut posted:i've always done the object first, then the descriptors. I like that too if I have a bunch of the same kind of thing (like several identifiers), but always snake_case for Python code
|
# ? Mar 9, 2022 23:55 |