|
I have a weird python question. I've been having weird internet outages, so I whipped up a little program that pings a given URL once every second, then logs any errors with a timestamp to a file. For the purposes of having a tiny visual to go along with it, I have it print a '.' for successes, and a '!' for failures to the console. This works fine in, say, the VSCode console, but it doesn't update at all when I run it in the windows commandline or powershell through python. When I had entire sentences for success/failure, it worked fine. If I make it so it logs a newline after every character, it also works fine (but does seem to only update every two characters?). Googling lead me to some weird regedit discussions on stackoverflow, but it didn't result in anything. For examples: Not working: code:
code:
|
# ¿ Jul 28, 2019 07:24 |
|
|
# ¿ Apr 28, 2024 23:47 |
|
QuarkJets posted:Does it work if you use other characters, such as 'a' and 'b'? No, doesn't change it.
|
# ¿ Jul 28, 2019 08:04 |
|
cinci zoo sniper posted:I’d recommend hacking tqdm like so for this use case. Hey cool, I'll check it out! For the main problem, mostly I'm just wondering if there's some weird python rule I'm not aware of (or alternately, weird console interaction I'm not aware of).
|
# ¿ Jul 28, 2019 08:28 |
|
Thermopyle posted:Yeah, anytime you have weird issues with stuff not updating in the console like you think it should start looking for the flush options. Foxfire_ posted:You're writing to stdout and it's line buffered by default. It won't flush to the OS unless a buffer fills up or you write a newline. Ah, thanks! That totally fixes it.
|
# ¿ Jul 28, 2019 18:15 |
|
I managed to land my first dev title (half dev, half my current specialty) in a mostly Python group at a big company. I know the first rule of software engineering is 'do what your team does as long as it's not insane' but are there any other tips in terms of like real-world stuff I should consider?
|
# ¿ Oct 4, 2021 18:26 |
|
Yeah, the way selenium interacts with the browser seems to rely on debugger functions that aren't generally on - you have to launch chrome specifically to allow Selenium to control it, for example. More info on the use case can help because I might be doing something similar.
|
# ¿ Jan 2, 2022 02:42 |
|
uguu posted:That's a good idea and possible. I wonder if a greasemonkey script might be able to do that. It ain't python though.
|
# ¿ Jan 2, 2022 09:01 |
|
On the topic of unit tests, are there any good books or sites on how to retrofit an existing codebase with unit tests, especially one that's reasonably large (around 8k SLoC) and involves a lot of external calls? I know that generally it's going to boil down to 'chunk it apart and start working on pieces' but it'd be nice if someone had already written some stuff from the perspective that I'm going into. Most of the stuff I've read highly leans into test driven development, which is a good paradigm, but I'm working on an existing product I didn't start on. We have a backlog item to write some unit tests later, and I'm interested in doing that.
|
# ¿ Jan 9, 2022 07:00 |
|
qsvui posted:Sounds like Working Effectively with Legacy Code by Michael Feathers might help. It doesn't use Python but I think the advice should be generally applicable. Book's on my buy list, but I suspect most of it won't be applicable; this codebase is only a year or two old, it's just big enough to be a pain to retrofit testing in. Edit: Looking around, actually I think this book is actually probably the right answer. Thanks! I picked it up. Falcon2001 fucked around with this message at 02:36 on Jan 10, 2022 |
# ¿ Jan 10, 2022 02:32 |
|
D34THROW posted:It's ridiculous how I get over a simple half-functional login page in Flask that centers the form on the screen and pins the header to the bottom. If I can get this poo poo on a Heroku instance and spread it around the company I'm gonna be quite happy and make a new name for myself other than "the Excel toucher" and "the NAV geek". This was me earlier today when I managed to automate the manual generation of some reports for work. Until about 4 hours later when I discovered a minor discrepancy in a 110mb csv and now I'm having to go back to the datasource all over again.
|
# ¿ Jan 20, 2022 01:06 |
|
former glory posted:That link is a pro click. I don't have a compsci background but am a dev, and I think that link is pretty great. I've been watching a lot of stuff around the arguments around OOP and this article does a great job of encapsulating some of my confusion around those arguments.
|
# ¿ Jan 23, 2022 20:55 |
|
I just discovered https://www.attrs.org/, has anyone used this in a shared environment? I'm torn between 'wow this looks amazing' and 'huh, I wonder if this is going to make readability weird for folks that aren't familiar with attrs'.
|
# ¿ Jan 30, 2022 09:12 |
|
QuarkJets posted:What kind of functionality are you looking for specifically? I'm looking at https://www.attrs.org/en/stable/examples.html and https://www.attrs.org/en/stable/why.html To start, nope, not using older versions of python, we're in late 3.X territory (3.7 I think?) and security won't let us fall behind into the terrible world. https://hynek.me/articles/import-attrs/ this goes into some of the reason why attrs still exists in a world with dataclasses, which apparently were built from attrs in the first place, but I suppose the other point is that in most cases dataclasses probably does most of what I'd use attrs for and is standard library; I've even used dataclasses on a project recently.
|
# ¿ Jan 31, 2022 05:05 |
|
It's not unreasonable or uncommon in my experience to have a function in a class that outputs a specific datatype for other use (as_dict, as_list, etc). I wouldn't necessarily throw all the formatting logic in there, because I think you risk overcrowding the class, but then again I just did that myself in a reporting project. In general, probably good to avoid having to double refer to data (maintaining a list of keys and values outside of the relevant class, etc) but I suspect others in here would have better advice. Also: Nthing the recommendation for typehints. They're an absolute lifesaver for saving you one more run when you forgot something simple like a return/etc, and they're not that hard to include. https://www.pythontutorial.net/python-basics/python-type-hints/ seems like an alright intro, and it makes your IDE a ton more readable. Falcon2001 fucked around with this message at 19:19 on Feb 7, 2022 |
# ¿ Feb 7, 2022 19:17 |
|
Dawncloack posted:I have to backfill information into an SQL database, and I am writing a python script for it. Can you just modify the data you get to or from that program? Because modifying timestamps to add precision (even fake precision) is probably a lot easier than figuring out a way to muck with your system time programmatically, although I'm sure there's a way to do it. Maybe give some examples of the input/output data? Falcon2001 fucked around with this message at 03:27 on Feb 13, 2022 |
# ¿ Feb 13, 2022 03:25 |
|
https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/ This is probably a pretty decent coverage of 'truthiness' in Python; python allows you to test some things. If you want to check if the variable is an integer, DefeatistElitist has the right idea.
|
# ¿ Feb 18, 2022 03:05 |
|
Some more details, now that I review your code: - Input() returns a string, so you'll need to try and cast that to an integer to do division. You probably want to add a try/catch clause to do like code:
Think about the flow there as well. You want to do a few things - Validate that the inputted numbers are integers, then ensure that the second number is not zero, then you can safely divide and return the value. I can be more explicit but I'm assuming you don't just want a response with 'here's the answer'. Some references. https://docs.python.org/3/tutorial/inputoutput.html https://docs.python.org/3/tutorial/errors.html https://www.programiz.com/python-programming/type-conversion-and-casting
|
# ¿ Feb 18, 2022 03:11 |
|
abelwingnut posted:so i'm trying to get python running on my win10 machine. what's the best way? this windows subsystem for linux seems pretty nifty but it also feels like it could be a disaster. as of now, i'm only trying to do web stuff, but could see myself doing more machine learning things. Don't get it from the MSStore, for whatever goddamn reason it has some weirdness with PIP and installers and things, just get it from Python.org. Seriously, just get the installer and make sure to click the 'add to path' option and you're gold. If you're just dicking around, you don't need to worry about pyenv or virtual environments or anything like that, until you get to the point where you're trying to pass code around to other people or package it/etc, and at that point you can always then sit down and do it. WSL2 is rad, but it's also not really a problem for Python; Python and Linux go hand in hand because a lot of linux distros come with Python so it's sort of a 'known value' in the environment, but it works just fine on Windows. VSCode has great support for Python btw, and does integrate into pyenv and other things when you get to that point.
|
# ¿ Feb 18, 2022 21:53 |
|
QuarkJets posted:That's a bad reason God I cannot count the number of times I've just been blinking at something I know I wrote a week ago. I've started making it a habit to improve docstrings even if I'm not heavily modifying the function.
|
# ¿ Feb 19, 2022 23:44 |
|
QuarkJets posted:Yeah this PEP has pretty good guidelines One thing I love about Python is how consistently people seem to adhere and agree to PEP8 and other style guide stuff. Like I dunno, I never noticed this when working on C#, although it's possible I was just new enough to not know it.
|
# ¿ Feb 20, 2022 21:25 |
|
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 |
|
teen phone cutie posted:huh interesting. even more interesting that the sqlalchemy docs don't even mention this: Yeah, in general you want to avoid reusing any already used variable names from the standard library. IDEs should warn you about this if you're using one, so naming something list, id, type, etc should all give a warning because you're overwriting it. In theory you can do it, but it can cause weird behavior or straight up bigger problems so it's a good idea to avoid it whenever possible. It's kind of wild that they just don't mention it in their docs, but welp.
|
# ¿ Mar 10, 2022 01:34 |
|
Protocol7 posted:It's probably fine on a class, if not a bit of a code smell - PyCharm correctly flags a global variable shadowing the builtin, but doesn't care at all in a class. Oh, hey that's a good point actually, I hadn't considered that.
|
# ¿ Mar 10, 2022 08:15 |
|
Sleepy Robot posted:I'm trying to piece together a script to automatically adjust screen brightness settings based on a user's location. So far I have it working with hardcoded lat/long values, but I'd like the user to input a city / state and it accurately determines the intended location (lat/long is required for sunset module) or at least offers a set of possible matching cities before converting to coordinates. Is there an API or package that can do this? I'm thinking Google Maps could probably handle it but maybe there's a better approach. https://openweathermap.org/price looks like it has a free tier, which depending on how much calls you're making should be fine. Ninja Edit: https://openweathermap.org/api/geocoding-api it has a geocoding api, it's not just for weather. Ninja Edit 2: https://geopy.readthedocs.io/en/stable/ It looks like there's a python library for interacting with geocoding apis too, which might save you a little work.
|
# ¿ Mar 11, 2022 01:26 |
|
12 rats tied together posted:If you have unique ids that would scream set difference to me, which is what you said yeah Agreed on set difference - sets are great for when you're trying to do unique IDs of things, and you can use them pretty lazily to just dedupe a list. (as long as comparison/etc is possible between the list objects, etc etc. There are edge cases.) code:
code:
|
# ¿ Mar 11, 2022 22:20 |
|
So I have a bit of a best practices question. In the event that you have a function that mutates a data structure like a list/etc, is it best practice to return the mutated version, or to just make it clear in the docstring that the function mutates the object? For the sake of argument, let's assume that this isn't a library or other externally facing function and is pretty tightly bound business logic specific to a situation, where you're separating code out into functions to improve readability/etc.code:
code:
|
# ¿ Mar 22, 2022 20:05 |
|
Thanks for the feedback! I think the biggest takeaway is that it should be clear, and I properly type hint all my functions/etc, so it's clear that this doesn't return anything (-> None), which means I think I'm going in the right direction. I also agree with the nicest option being the 'make it a class and then have a method that does the thing to itself'. This is a four year old codebase that I just came into, so there's a few different places we're just passing dictionaries around instead of using dataclasses/etc. Some of those I'll be trying to clean up as I go along and generally docstring or otherwise fix up some of the older bits of this program.
|
# ¿ Mar 22, 2022 21:00 |
|
Armitag3 posted:Apparently this works https://peps.python.org/pep-0498/#specification it's in the spec, yeah. I think I had to use this recently.
|
# ¿ Mar 24, 2022 00:28 |
|
Speaking of pickling: We have a (currently) single threaded job that runs regularly and operates as a pipeline where it starts by gathering data from multiple sources, and then merges and transforms those into a composite file we use for the rest of whatever job is running. I'm looking into ways to make this more flexible, so I'm debating setting up separate jobs and setting up a 'cache' by pickling the composite at the end of merge and then storing it for reuse, and then regenerating it whenever the stored data is more than 30 minutes old. This would cut about 60-75 seconds off of each of our jobs (minus the whole unpickling process). Is pickle a reasonable tool for this use case or should I be considering something else? The data is not easily serializable to json since it's fairly complex and includes a lot of custom data types.
|
# ¿ Apr 2, 2022 22:13 |
|
lazerwolf posted:This was my intent. I’d like to ensure the “demographic” dict has a certain form. This might be the only function that would use the dataclass but I want to utilize serializer functionality in non DRF context. If a dictionary leaves the scope it was made in, it's a good candidate for sticking in a data class, IMO. I took over some software and we have a couple places where it's just like code:
|
# ¿ Apr 14, 2022 05:48 |
|
QuarkJets posted:Most linux flavors ship with a system python (and make a ton of python packages available via yum or apt) for exactly this reason. Python is pretty handy and has a lot of cool stuff built from it, so it's reasonable to make that stuff available from a standard system location for multiple users to use. So if you have some Python package that you want to make available to your users, especially users who aren't doing their own development, putting it in the system location is a-okay in my opinion. We were doing a cache stored as json and handled as a dict, and I just finished refactoring in an abstraction layer and got to delete so much weird code that was all around handling these weird fragile dictionaries, and bonus, it's gonna make it much easier to switch that over to a proper storage system like DynamoDB. Falcon2001 fucked around with this message at 07:30 on Apr 14, 2022 |
# ¿ Apr 14, 2022 07:25 |
|
Falcon2001 posted:dataclass talk Actually, I should expand on this in case anyone is newer and reading this, like I was not that long ago. So it is extra code and extra work to define data storage classes, so there is a tradeoff here. But what I'm poking at here is the idea that a lot of times you use a dictionary because it's convenient, because putting things into a dict and taking them out is a reasonably easy operation. Especially with certain external operations it's easy to just go 'oh yeah, json turns to dict, I'll just keep it.' and that's not always a terrible idea. If the json schema is well defined, it's probably fine; you'll just need to look up the schema later. But the bigger problem is when you start making your own dictionaries to hold data. However, the tradeoff is that new development turns into a constant fight of 'wait what the gently caress is in here. What are the keys? Let me run a debugger. OH okay that's what I want.', which isn't the case if you define a class, because your IDE is going to tell you the attributes of the class readily, and even if you don't use an IDE, you're one hop away from just looking up the code yourself. (This part is like CS101 but putting it out there as an example) The other upside to using your own classes for data structures is that it gives you all the OOP benefits of classes so it can be as lightweight or heavy as you want it to be. For example, I want to cutover from a json flat-file for storing our cached data to DynamoDB or another lightweight database soon, and by adding an abstraction layer to this, I was able to ensure that the rest of my code is not going to need significant overhauls in the future, since instead of dictionary style syntax, we're using more general syntax, and some helper methods. Whenever we cutover to a new storage system I can just update the new cache_types I defined and keep all my changes pretty confined. That all said, there are some downsides, namely that it is more code to write, but like most coding, it's one of those tradeoffs between today vs tomorrow.
|
# ¿ Apr 14, 2022 15:30 |
|
QuarkJets posted:Sure, I agree with all of that. FWIW as the recent dataclass lover, I agree with all this, because mostly the idea is 'if you're going to be passing around data from place to place as a clear set of data, you might want to schematize it'. I'm mostly arguing against dictionaries, not against overuse of classes/etc. If you're just passing info from one place to another, then yeah, just pass them as arguments. For example, the 'responses' dictionary I was complaining about is data from a specific process - it's not going to change regularly (or if it does, it's going to gently caress up our schema enough for a code change anyway), and we basically pass it along a pipeline into multiple places. By the time I'm into the fifth step, there's no easy way for me to go find where the heck it's built anymore without walking back the code a bunch. ExcessBLarg! posted:It's a common trap to have an Everything class that contains attributes for everything and gets passed around to every function to pick the parts they care about. Yeah also this would definitely be bad and I agree with you...except how much worse would it be if it was a dictionary instead. Falcon2001 fucked around with this message at 04:56 on Apr 15, 2022 |
# ¿ Apr 15, 2022 04:53 |
|
punk rebel ecks posted:So I'm back from my traveling. To add onto what others have said: - Absolutely use a linter of some kind, like Flake8. This will significantly increase your code's readability and uniformity and help reinforce good coding habits. This also isn't a 'newbie crutch' thing either, all the professionals I know use Flake8 as well as auto-formatters like Black or autopep8 to ensure that you're sticking to consistent results. - Breaking apart things into modules is not only good for readability, but also good for writing code. There's a lot of various styles or opinions on this, but In general, split your code out into modules based on what they do - you'll often see a types.py file in some smaller projects where you group general data types, or bigger classes can have their own files. You can overdo this, but I mostly go by 'feel'; when a file has gotten so big that it starts to feel clunky, it's a good sign that I need to look at refactoring some stuff out into it's own class. - Also, I know that you've gotten some pretty significant critique here, but I do want to say that having anything on GitHub is pretty good, and being able to point to a finished project and say 'hey I made a thing, and it works!' is something to feel pretty good about. That said, having a project that addresses the critiques that everyone's brought up is an even better starting point.
|
# ¿ Apr 23, 2022 08:17 |
|
Depending on where they are/etc, he could also look for smaller shops that are hiring positions that do coding AND other stuff too, like sysadmin roles or whatever. As long as there's some coding, could be good experience. Although I'm sort of unfamiliar wiht how that tht space looks these days.
|
# ¿ Apr 24, 2022 02:53 |
|
Also, a BS degree is still useful even if it's the same field, because it shows a certain baseline level of 'I can accomplish something concrete." and talks about general knowledge as well.
|
# ¿ Apr 24, 2022 05:10 |
|
SirPablo posted:List comprehensions are awesome but they can get really wonky to read sometimes. It might be easier just to write your code then go back through and find where you are doing any "for x in range ()" type loops. Those are prime candidates for list comprehension. I think list comprehensions are one of those very easy to gently caress up 'clever coding' things, but they're also much clearer sometimes, so you have to weigh the balance. Once you get into nesting them it's often much clearer to use a nested If statement instead or something.
|
# ¿ Apr 24, 2022 16:32 |
|
lazerwolf posted:I will preface this by saying I don’t have experience with dash (I’m not sure about the quality of their docs), but I would try the official documentation. At the very least, starting with the official documentation is a good way to make sure you're referring to a good source of truth; if you have questions from that, you also end up in a much better place to start googling from once you know the terminology used in that particular project/etc.
|
# ¿ Apr 25, 2022 05:10 |
|
worms butthole guy posted:I have a question about what the best "stack" would be to do this stupidly simple task. Depending on how much you want to try a new tech, I've been seeing HTMX a lot lately as a JS-less (but built in JS) library that seems to be a nice lightweight alternative to vue/react/etc. I haven't tried it myself though, and I'm curious how other view it, but the videos/etc seem pretty promising. edit: here's a video example that seems to at least be somewhat near what you're looking at : https://www.youtube.com/watch?v=kNXVFB5eQfo&t=1079s Falcon2001 fucked around with this message at 23:40 on May 2, 2022 |
# ¿ May 2, 2022 23:38 |
|
|
# ¿ Apr 28, 2024 23:47 |
|
I've used that same concept with dealing with templates without kinja for simple email forms at work; we just load the template and then .format() it. But everywhere else is fstrings.
|
# ¿ May 7, 2022 08:07 |