|
SurgicalOntologist posted:That reminds me, I have an ongoing argument with a colleague about private (by convention) methods and attributes in Python. He wants everything to be private unless there is a strong reason to make it public. His classes typically have only one or two public methods. I prefer everything to be public unless there is a good reason to make it private. I typically only make private methods for little helper methods that I refactor out of other methods. I see this kind of thing playing out in Rust (where, eg, variables are assumed to be constant unless declared differently) vs C (where do what you will is the whole of the law). IMO it comes down to "would you rather your code be safe or flexible?" I'm not hugely involved in the Python world outside my job, but I think most folks agree with your style, but if "I don't want to change" was taken out of the math we'd probably all follow your colleague.
|
# ¿ Oct 22, 2022 13:38 |
|
|
# ¿ Apr 28, 2024 20:31 |
|
I have a Python memory management and speed question: I have a Python program where I'm going to be reading in a stream of data and displaying the data received over the last X seconds. Data outside that window can be junked, I'd like to optimize for speed first, and size second. I know how I'd do this in C, but I'm not sure about Python memory management. The data are going to be in a live updating graph, so accessing it contiguously would be good. I have my own ideas about circular buffers or offset pre-allocated buffers, but I have the feeling Python has something off the shelf that will handle this well. Does anything like that exist?
|
# ¿ Feb 11, 2023 14:50 |
|
That can give a divide by zero answer,, but otherwise is better. The question is easy enough but the ChatGPT answers are pretty cryptic. Thanks everyone for the Python tips, I'll definitely play around with deque.
|
# ¿ Feb 12, 2023 23:22 |
|
Soylent Majority posted:Thats the basic poo poo I need - now I have something far less dumb: Lists and iterators are absolutely massive and central to Python. When you're trying to do something in Python, first say "how can I use lists here", and you'll probably come up with a better solution.
|
# ¿ Feb 15, 2023 05:47 |
|
I'd like to read streaming data in with Python and have it live update in a line graph. Does anyone have experience with this? Plotly and Matplotlib have animate functions, but I worry that my use case is a little outside what they normally handle. The Plotly examples I see use static buffers, and I need to handle streaming data
|
# ¿ Feb 23, 2023 03:13 |
|
Does anyone know any examples of well put together GUIs in Python? I'm mostly a Python hack, and I've got an older wx Python program, and it feels like the presenter (wx.App object) is doing too much work. Like I'll have a separate object that keeps a list of books, and then get_scifi_books() will be defined in the Presenter object. Maybe it doesn't matter because Presenter is the only one using the function, but it feels like I could add in more testing and stuff if get_scifi_books() was defined in a separate object that keeps the books. But then that leaves Presenter mostly empty. How far does that rabbit hole go? If I want to have 3 buttons for get sci-fi, get history, get educational books, should I just have the book object return a list of button names and the associated functions, and the Presenter just blindly puts the list of buttons together?
|
# ¿ Mar 7, 2023 21:40 |
|
I've heard good things about Kivy but haven't tried it. PySmpleGUI sounds right up my alley for a lot of the basic, internal stuff I'm doing. This is all for running local programs so flask may not be the best. I'm a little worried that WxPython may fall seriously behind. It didn't move to python 3 until 3.5 or something, it sounds like not many folks use it, and it just looks like Windows NT. If I want to do fancy guis, is qt more the way to go or should I just not use python?
|
# ¿ Mar 9, 2023 04:29 |
|
Twerk from Home posted:How much Python 2 are y'all living with? What's a sane expectation for carrying along Python 2 applications that don't have any change planned but are expected to keep working forever? You are going to run into trouble hiring, and searching for answers will become harder and harder. You need to update. Interns should be able to do a big chunk of it, unless maybe you're doing something extra strange with low level objects or something.
|
# ¿ Mar 16, 2023 20:29 |
|
Zugzwang posted:How reliable are modern Python-to-exe packages? I recently learned that a research tool I’ve been working on for my job might eventually need to be deployed to outside users, probably closed source. And due to IP reasons on their end, it can’t be a web app hosted on AWS or whatever because their data/results can’t be on a system outside of their company at any time. - Including non-Python files takes some effort, like if you need to load in some settings from a .json that you expect to be included in the EXE - Create the exe from a venv that only has what you need, otherwise it may be huge - It is fairly trivial to reverse "compile", the scripts are just zipped up with an Python executable, so don't expect to have any secrets there E: ^^^ I'm curious, do you work in academia? What place has people that complain about needing to use Python 3? StumblyWumbly fucked around with this message at 00:17 on Mar 17, 2023 |
# ¿ Mar 17, 2023 00:14 |
|
I'm making some code where I want to put in a target sensor and setting, and get out the channel ID and parameters it should be tested against. So I put in {"Microphone": {"sample_rate": "20000", "gain": 1.4}}, and I get 17: {"noise": 0.07, "rate_tolerance": 0.001}, where 17 is the channel ID. I have some sensors that can have multiple channel ID's associated with them. I have the system working using just dicts and functions that go through and map from one format to the other. The dicts work great as long as you remember all the keys, I'm trying to move them to objects to help structure the data more. Moving this into objects works great when one target sensor becomes one set of test parameters, but I feel like I'm missing an easy way to handle one target sensor becoming multiple sets of parameters. What I have now is: code:
I feel like there's a Pythonic way to do this that I'm missing. Maybe TestObject should be an object generator function that always returns a list of the test objects (which is normally going to be length 1), and I always iterate through that list and add the elements to the param_dict? Maybe I should ditch the dict and have an object that holds all the test parameters so I can manage access? Am I missing something clever here?
|
# ¿ Apr 1, 2023 16:34 |
|
Thanks, I did not remember dataclasses exist, so that's useful. I guess the no-background question is: I'd like to be able to parse through a dict, and generate a list of objects, all the same type, for each item. These lists will all get merged into one master dict of the resulting objects. Should I just use a function to generate that list of objects and merge them into a single dict, or could I do something with the object design itself so it is a bit more self contained? The more I think about it, the more I think the generator function that puts out a list of objects is the way to go.
|
# ¿ Apr 1, 2023 17:51 |
|
Neat, Enum looks very similar to the mapping system I have, but I'm also not sure why I would prefer Enum over the current option. What I have is pretty much:code:
I think I could replace that system with an Enum, and I don't think I'd need to change anything that much but I'm also not sure what that would improve? Is it just a matter if the map being more rigidly structured so I don't need to remember what strings to use, or is there something else it opens up?
|
# ¿ Apr 1, 2023 18:25 |
|
ziasquinn posted:I've decided to try and make another "run" (used loosely here) at learning Python, should I just go through Think Python and Python Docs or are there more recent kind of books/guides/docs for starting off? I know about "Automate the Easy Stuff" and kin, for example. Do you have a project you'd like to try doing? Like automating some spreadsheet work or renaming files or doing math? And what's your programming background? There's no one size fits all.
|
# ¿ Apr 9, 2023 14:05 |
|
Using ChatGPT for regex is great, AIs are very good at that kind of translation. But, if you can't figure out the rules you need for reformatting the data, you are pretty much doomed. Since you've been cleaning the data, you probably know the rules, you just haven't formalized them. Hopefully you can get a program to do most of the work, and then manually, eg, delete garbage data at the start of the recording. Out of curiosity how are you controlling these instruments now? VISA bus? Or truly custom software?
|
# ¿ May 9, 2023 00:10 |
|
Falcon2001 posted:All programming is an exercise in 'time saved vs time spent' - https://xkcd.com/1205/ is the classic example, but I'd say that sort of work would bother me personally.
|
# ¿ May 10, 2023 03:33 |
|
This might be similar? Our board assembly house tests the hardware with some code we provide, and getting them updates has been a pain for multiple reasons, so I set up some stuff so they can pull the latest version of whatever they need from GitHub. The steps are install Git, run a. bat that sets up the username and password (a fine grained PAT with access just to that repo), and clones the repo with the tool they and other scripts they may need (eg to pull the latest repo). Should work well, but the meeting to get them running is tomorrow. If you definitely need a web based answer, I think the computer would need something special installed so the web app can interface with the serial or USB or w/e
|
# ¿ Jul 6, 2023 00:14 |
|
My company sells sensors, and we have some open source Python libraries for configuring and interfacing with them, but we'd like to make some advanced wireless features that we can sell as subscription software, because that's how the world works now. It seems like the way to paywall a Python library for Windows would be to have a separate server running out of an install/EXE, which does the real work including handling the licensing and communicating with the device, and the Python library would just be an API interface with the local server. Qoitech (https://www.qoitech.com/) does this for automating an interface to their OTII power supplies, and it seems to work ok with some issues (RasPi support seems janky, folks keep forgetting to log out and lose their license, etc). The issue with just doing it all in Python, of course, is our secrets are completely exposed. Has anyone seen a better way to do this?
|
# ¿ Jul 7, 2023 14:05 |
|
wolrah posted:So you're saying you want to charge a subscription for a local device talking to a local network service that costs your company nothing ongoing? There's a lot of details I'm not providing because this is just a noxious conversation to have. I hate capitalism too. QuarkJets posted:You said that the libraries are open source, so what secrets are you trying to protect? If you mean "secrets" as in authentication details then those shouldn't be in your codebase anyway.
|
# ¿ Jul 7, 2023 18:48 |
|
The main threat would be folks trying to access features on the device that they don't have a license for, so we can do the actual check on the device, which is very secure. I'm not super strong on cryptography, it feels like we might be doing enough out in the open that the customer can figure out our magic crypto numbers. We'll look into that, but I'm not super worried about it since our customers are mostly businesses.
|
# ¿ Jul 7, 2023 21:16 |
|
After a lifetime of stashing stuff in dicts that are painful when I look at them 2 weeks later, I'm thinking of just going whole hog with dataclasses. I get into a lot of situations where I have 20 pieces of hardware, each with a sample rate, range, and enable/disable, or a block of data with a start time, end time, format, 10k samples, and a few other features. Is there any drawback to using dataclasses more? It seems too good to be true. Anything to consider in going with Pydantic vs dataclass?
|
# ¿ Aug 4, 2023 22:18 |
|
Exactly what I was hoping for, thanks!
|
# ¿ Aug 4, 2023 23:23 |
|
I've used multiple iterations of PyCharm, and they've all just worked for me. One drawback to PyCharm is that there are so many releases, sometimes advice doesn't always work for your particular version. Is something weird about this computer? Did you install python 2.7 or muck around with the path? Can you run scripts and get them to just print stuff? Is there weird security stuff or some kind of Python framework thing? Are you sure you are pushing the right buttons and setting break points in the right files? Hopefully those questions spark something because without sitting at the computer it'll be hard to debug.
|
# ¿ Aug 17, 2023 05:10 |
|
Just today, I put in the most basic rear end updates into some code I wrote like a year ago. The file already had the most basic testing possible in it, and I saw it and said "sure, I guess I'll check my work" and found like 3 errors. Because I wrote those tests a year ago, I was able to finish half a bottle of wine before my long weekend started. So proud of who I was, that man is my hero
|
# ¿ Sep 1, 2023 04:10 |
|
huhu posted:I have two classes Plotter and Layer. A Plotter consists of an array of layers. Each layer is an array of instructions. A pattern I like (could be bad, actually, and I'm interested in other opinions on it) is to have add_line return itself, so if you want to add multiple lines you could do: code:
Depending on how the code grows, this can make life better or worse.
|
# ¿ Sep 25, 2023 22:31 |
|
Gantt chart? Or you want something that covers stored values that should not overlap?
|
# ¿ Oct 15, 2023 21:24 |
|
BUUNNI posted:Thanks for the suggestions! Right now I'm looking for help dealing with creating scatterplots using PyPlot (for instance, creating a plot that shows 'UV Index vs. Population Density in World's 20 Largest Cities' with the dots proportionate to city population), and handling dataframes using Pandas, including creating data columns, creating numeric variable from column data, etc... I've found that Plotly makes better graphs more easily compared to Matplotlib.
|
# ¿ Oct 19, 2023 00:11 |
|
Falcon2001 posted:Speaking of plots and datavis/etc, I'm curious how people would approach this problem: Have you tried using DataFrame.iloc calls with Pandas? That can let you have weirdo indices and also check neighbors
|
# ¿ Oct 19, 2023 15:06 |
|
Two tips for writing list comprehension: include a comment about what you're trying to do, and use descriptive variable names, even if it triples the number of characters. List comprehension are often obvious only while you are writing them. Also it seems like the kind of thing chatgpt would be good at, but I have not tested that
|
# ¿ Oct 21, 2023 13:06 |
|
It's frustrating because it almost fits in a comprehension, but I think the end condition means it won't.
|
# ¿ Nov 10, 2023 23:00 |
|
I'm writing a bunch of PyTest code to run some integration tests. We have a top level test dispatcher that sends out some JSON parameters to a system that runs the code, then we use PyTest to check that everything ran correctly. We need the PyTest code to have access to the parameters, so the parameters go into the execution environment, and in conftest.py we grab the test parameters and parameterize them to make it accessible to the individual tests. This feels like an inelegant solution to a standard situation, so I wonder if there's a better solution. Specifically: - Am I over using conftest.py? Is it really the best solution to manage input parameters or is there something I'm missing? - As multiple people add tests, we have a bit of disagreement between philosophies of "Pass an object containing all parameters to each test, let the test grab what it wants" vs "Separate each test parameter to its own pytest parameter in conftest" Are there standards I should pay attention to here? - Should I just not use PyTest at all? Our test case is more "Check that multiple files meet the parameters provided", not "check one thing against multiple parameters". I like PyTest in general but it definitely feels like an awkward fit here.
|
# ¿ Nov 26, 2023 14:45 |
|
monochromagic posted:This does seem inelegant, and I'll freely admit I'm not quite sure what you're trying to solve here. I'm assuming that "parameters go into the execution environment" means something like passing them as environment variables. I'd use fixtures picking them up instead, rather than trying to use parametrize because that functionality is more for unit tests. With respect to separation, I prefer to separate/isolate tests as much as possible so I wouldn't pass an object around with all parameters in any case. Right, the problem we're solving is testing out sensor hardware. The input parameters say, essentially, "take measurements on channels <A, B, C> at rate <X> for <Y> seconds, <Z> times". Other code runs those parameters on the sensors, this will give us Z files, we want to make sure channels A, B, C are present, with the specified rate and duration. In reality channels A, B, C will each have their own test criteria, like max and min values. We group all those channel specific values into the test parameter object, and in conftest.py we have code like: Python code:
- Set the test order so we open the file, test all channels on it, then close the file, to minimize file overhead (not sure if this is a real problem) - Each test gets the file, channel id, and the test parameters. The test will use the channel ID to pull its data out of the file, and get its parameters from the test parameter object. Parameterizing it at a higher level seems like it would increase overhead a lot - I _think_ we parameterize the test parameters here because we may change them with command line arguments, or maybe we wanted to just generate the parameter object once? Since these tests are linked to hardware, they all run on Raspis, so compute power is not huge, but I am going through some steps that reduce isolation, so maybe I should re-evaluate that choice. In general, I might be better off if I just always write the test parameters to a file, and use a standard fixture to pull it out, instead of trying to handle command line vs environment. I'm not sure on the best way to handle splitting up the file data or input parameter data without making more of a mess.
|
# ¿ Nov 26, 2023 16:41 |
|
Ok, thanks, sounds like things are a little ugly but I'm not missing anything big. Changing the file format is a neat idea but not possible because the format is part of the test, and pre-processing it would be a big change. We are using a dataclass for the test parameters. I may move it into Pydantic for a little extra checking on input parameters.
|
# ¿ Nov 26, 2023 19:43 |
|
I don't want to sound old, but what's up with kids today and their async statements? My real question is I have some code running in a gui, my model may be modified by mouse events and I'm pretty sure the gui backend will make sure mouse events and display won't overlap, but I'd like to be sure so I wanted to add a mutex. Being lazy, I thought I'd just add a threading mutex, since they should all be pretty much the same, right? That's when I found asyncio.Lock. The documentation says it is "Not thread-safe", but I think it must mean you may need to wait while accessing it, as opposed to ROM which is perfectly thread safe? I feel like I'm clear on the differences between threads and async. async seems neat and different, but a bit jankier than threads. Is there any real difference or preference between the threading.Lock vs asyncio.Lock? At a low level, they must be doing the same thing, but I could imagine differences in what happens when you need to wait for the lock.
|
# ¿ Dec 3, 2023 21:47 |
|
Thanks for the help, that clarifies things. I think I expected async to be more just because it's new. Is it true that if I have thread lock protecting a resource that ends up getting used in async, things are protected? Or is there a potential issue because an async lock would automatically yield or await but the thread lock will not? This question is purely academic
|
# ¿ Dec 4, 2023 23:19 |
|
Vulture Culture posted:You need to be really careful addressing synchronization in programs that are simultaneously using asyncio and worker threads. Trying to acquire a locked lock will block the thread, right? And as a general rule, you don't want to put blocking operations into the event loop. If you spin up worker threads for every synchronous task that could possibly hit a blocking operation, including working with thread-locked resources, you're fine. Ok, thanks, I see what you're getting at. Thread locks will bounce you to another thread, but because of their tie to the thread it wouldn't make sense to send you to another asyncio. I guess the other part is that since async won't yield unless explicitly told to, it doesn't make sense to have an async lock unless you do an async yield. My background is in FPGAs and microcontrollers, where things are just different. It seems like asyncio is mainly for servicing high latency calls like network queries. Maybe they could be used to help with multiprocessing (which I haven't used) or maybe CUDA, but I assume those systems already have features so async isn't necessary. Is there a use case for this that I'm missing?
|
# ¿ Dec 5, 2023 05:34 |
|
One thing to be slightly careful of is that Pydantic released v2 in 2022, so some internet info is outdated. Most significantly, ChatGPT is pretty much unaware of Pydantic v2. It's a pretty straightforward tool tho, like Dataclasses but safer
|
# ¿ Dec 25, 2023 16:32 |
|
Is using docker to do builds with pyinstaller a dumb idea or a great idea? It seems like a venv could work just as well but there's always something weird going on
|
# ¿ Feb 13, 2024 21:24 |
|
What I mean is, we use pyinstaller to make executables for our stuff. It always seems like this is a very delicate process because we have dlls and special files to include, so I've been encouraging folks to set up a docker that can make the build. I feel like a venv should fix these problems but it also sounds like it hasn't in the past, and I'm not sure if I'm remembering the ancient past or if there are things that just work better outside a virtual environment
|
# ¿ Feb 14, 2024 13:11 |
|
Seventh Arrow posted:Actually, I think I can probably even do everything in a single line: Your code is correct and generally better, but simple is not always shorter. Using that type of if statement is generally just unnecessarily dense. In some places like list comprehension I think it can make things more efficient, but if you can use a standard if statement instead you probably should
|
# ¿ Feb 15, 2024 13:55 |
|
|
# ¿ Apr 28, 2024 20:31 |
|
I'm playing around with Polars and this seems to workcode:
Worth noting that polars does away with an explicit index column, but has some fast and powerful filtering. E: Also worth noting ChatGPT is poo poo at helping with Polars because it is too new and re-uses some common terms.
|
# ¿ Feb 21, 2024 03:52 |