|
D34THROW posted:In my latest bit of surprising-myself code, I realized that a function I was writing with @singledispatch and would have violated DRY with could be boiled down quite easily - it takes an id, start date, and end date, builds a query, and executes it against the DB. returning the Decimal result. I HAVE COMMENTS Python code:
Python code:
Python code:
Python code:
If you use isinstance instead of type(name) == str then you can also accept string-derived types Python code:
Python code:
You've got a lot of repeated code here, you could simplify it a lot. Try this: Python code:
Python code:
|
# ? Jun 4, 2022 05:40 |
|
|
# ? May 15, 2024 15:56 |
|
Will having programming certificates from LinkedIn in specific things in Python help me at all in terms of a job interview? Like with Flask and other stuff? I understand my code in my portfolio is what matters, but I'm hoping they may catch some people's eyes. My job mandates me taking any LinkedIn certificate/classes (luckily they are all paid for).
|
# ? Jun 4, 2022 08:25 |
|
punk rebel ecks posted:Will having programming certificates from LinkedIn in specific things in Python help me at all in terms of a job interview? Like with Flask and other stuff? i doubt it. i always forget linkedin certificates are a thing when looking at a candidates profile and when i see them i just scroll past without a second thought.
|
# ? Jun 4, 2022 08:29 |
|
QuarkJets posted:I HAVE COMMENTS I didn't like this either, I was gonna change it today. quote:You could probably avoid creating this lookup table every time that this function is called and just define it inside of whatever kind of object Job is. If you define __getitem__(self, name) to return the appropriate attribute given a specific label, then you can make the mapping part of the class. Kind of like this: I realized upon reading this that I'll be referencing that mapping outside of that function, but all the functions that use that mapping will be in that module, so it'll probably end up a module-level constant, otherwise it'd be on every instance of Job and there's...a lot of those since it's a record from the ORM. quote:This does nothing, you probably meant to use single equals That was causing errors and I fixed it quote:If you use isinstance instead of type(name) == str then you can also accept string-derived types This in particular was giving me trouble. I mess around in the interactive Flask shell before committing anything to code and I just could not get it to work. quote:This is ok just be aware that None is already the default, you don't have to specify it I changed that too already. quote:You should always be explicit when checking whether a value is actually None rather than checking its truthiness, by literally using "x is None" or "x is not None". Checking truthiness can give you unexpected results; a valid input may happen to be falsy That's already been pared down and simplified to a pair of if statements that append a filter to the query if the date is provided, but I will take it from an if date_end to if date_end is not None for sure. quote:I don't see where db, func, or Job are defined; globally-scoped variables in a function call is a code smell. Constants are the exception. They're in the imports at the top of the module db comes from application, func from sqlalchemy.sql, and Job from application.models.data_models quote:I could be mistaken, but it's very likely that what you actually want here is db.session.execute(query).first(), or some other method that provides just 1 row. This line could be returning a lot of rows needlessly, and if the database is big enough then you'll experience performance issues from lines like this one I've mucked around with several methods in the REPL for this and that specific method is getting what I want. I don't like it either but for now it is what it is. all() returns a list of tuples, first() returns a single tuple; it probably has to do with using func.sum even though it's loving annoying. I appreciate y'all. I don't have a team, ergo I don't have anyone else to tell me I"m being a dumbfuck with my code EDIT: I'm getting even cuter now. Removed the ability to pass a str as an argument and got cute with the mapping, using the models as keys. That way, I can eschew the isinstance() check. Python code:
D34THROW fucked around with this message at 17:31 on Jun 4, 2022 |
# ? Jun 4, 2022 16:17 |
|
D34THROW posted:I didn't like this either, I was gonna change it today. In most sql frameworks first() and all()[0] are equivalent, try dropping the square brackets from first().
|
# ? Jun 4, 2022 17:59 |
|
QuarkJets posted:In most sql frameworks first() and all()[0] are equivalent, try dropping the square brackets from first(). I would be careful calling these equivalent! The underlying SQL query would not be the same. The resulting object should be equivalent, but what actually happens under the hood is not. The first method would be the unobjectionable better approach here.
|
# ? Jun 4, 2022 18:21 |
|
QuarkJets posted:In most sql frameworks first() and all()[0] are equivalent, try dropping the square brackets from first(). SQLAlchemy is being a gently caress. Probably because the raw SQL is along the lines of SELECT sum(...) as sum_1 and it's returning a row as opposed to a single value. Python code:
|
# ? Jun 4, 2022 18:30 |
|
necrotic posted:I would be careful calling these equivalent! The underlying SQL query would not be the same. The resulting object should be equivalent, but what actually happens under the hood is not. Yeah, that's what I said... (the returned row is the same, the difference in query (and performance) is why I suggested to use first() instead of all()[0] in the first place) D34THROW posted:SQLAlchemy is being a gently caress. Probably because the raw SQL is along the lines of SELECT sum(...) as sum_1 and it's returning a row as opposed to a single value. Ah, you're summing; that piece makes more sense now. Still, if you ever have a line that reads as all()[0] then you should just replace that with first(), they're going to give you the same result but first() is a lot more performant.
|
# ? Jun 4, 2022 18:50 |
|
QuarkJets posted:Yeah, that's what I said... I see that now in your preceding post. I just saw the one I quoted in isolation.
|
# ? Jun 4, 2022 20:44 |
|
Has there been some discussion on messed up dependencies? I bet there has been. Essentially, I have a 3.8 version with MySQLdb installed and working, but I tried to install Matplotlib and it is not recognized. Same for NumPy, with shoes in the distribution directories. Can someone point me to some resource to properly organize python dependencies? Ideally I'd like to install 3.10, erase previous version and make the modules work. I am willing to read and learn on my own but nothing I've found online has quite helped me. Thanks in advance.
|
# ? Jun 15, 2022 13:39 |
|
Dawncloack posted:Has there been some discussion on messed up dependencies? You should be using an environment manager. These some pros and cons to easy. In general if you're having to manage installing binaries anaconda is usually your easiest bet. This was you can have projects on python 3.6, some on 3.9, whatever. Likewise if you need to install some software, often someone has packaged the binary such you can conda install [thing]
|
# ? Jun 15, 2022 14:24 |
|
Dawncloack posted:Has there been some discussion on messed up dependencies? MySQLdb is an old Python2 package, whatever you have installed must be one of the many forks of that with Python3 support added. Is this your MySQLdb? https://mysqlclient.readthedocs.io/index.html If so, that's pointing to this pypi package: https://pypi.org/project/mysqlclient/ Use pip to check what packages are installed, maybe try updating this one Alternatively, the easiest/standard way to manage dependencies (including non-python dependencies like MySQL) is to use conda environments. Conda environments are created and managed with the command line tool "conda", or its faster replacement "mamba". Install mambaforge. Create a new conda environment for python and whatever other packages you want (mamba create -n new_environment_name python=3.9 matplotlib numpy) Activate the environment (conda activate new_environment_name) If you need to add other packages to it, once activated you can use "mamba install package_name" or "pip install package_name". The big repositories for conda packages are conda-forge and anaconda, you can easily search those places if you are looking for something QuarkJets fucked around with this message at 18:30 on Jun 15, 2022 |
# ? Jun 15, 2022 18:26 |
|
This forums rule,and you guys rule. Thanks!
|
# ? Jun 15, 2022 20:01 |
|
Dawncloack posted:Has there been some discussion on messed up dependencies? Use a virtualenv, theres a few different flavors but Pipenv is popular although it can be temperamental. Also, I need to tell you about our lord and savior, Postgres. Postgres loves you. He just asks that you accept him in your heart.
|
# ? Jun 17, 2022 02:35 |
|
duck monster posted:Postgres. Postgres loves you. Jokes aside, yeah, the peepz I'm doing a thing with have already decided to ditch MySQL in favour of Postgres, come next iteration. I took some time to figure out users and permissions at the beginning but it is super neat.
|
# ? Jun 17, 2022 12:46 |
|
I'm working with very large lists-of-dictionaries where those dictionaries each include a large 2D numpy array of floats (amongst other keys). I have a few processing steps where I want to sort this list into two lists based on a certain condition on the numpy array - basically a keep list and a reject list - the logic here isn't challenging, but in terms of speed and/or memory use is it better to use a modify-in-place approach (iterating over the list backward and popping the unwanted elements) or creating a new list via comprehension? I realize there may not be a universal answer here.
|
# ? Jun 17, 2022 17:36 |
|
Roughly how big are your lists? How often do you anticipate having to actually modify them (e.g., "rarely" vs. "frequently")? You might have to benchmark it.
|
# ? Jun 17, 2022 18:30 |
|
ExcessBLarg! posted:Roughly how big are your lists? How often do you anticipate having to actually modify them (e.g., "rarely" vs. "frequently")? ~200,000 entries each containing a 256x256 image. There are 3 steps I perform. First loop is a zero-check (np.all applied to each image); second loop is more complicated as I'm checking certain pixels ranges for certain features; then finally I resize everything to a new size (~80 x 80). What I've been finding is that my loops start fast but slow to a nearly-unusable crawl toward the end and I'm not really sure why.
|
# ? Jun 17, 2022 18:37 |
|
Cyril Sneer posted:~200,000 entries each containing a 256x256 image. Assuming that you're not discussing a major overhaul that would take a long time to code, I'd do...both and https://docs.python.org/3/library/timeit.html. Basically I suspect that you're trading memory utilization for speed - creating two new lists is going to probably use more memory (although not a ton if my understanding of Python references is correct). On the other hand, reverse iterating and popping is probably not a lot of overhead since you're basically doing O(n). If your loops are crawling at the end I'd measure memory usage or figure out if something isn't getting cleared, or if you're doing some sort of extra iteration or something, like checking your good/bad lists each time you add something to them.
|
# ? Jun 17, 2022 19:26 |
|
Wouldn't a pair of list comprehensions be O(2N) while popping is O(N)? You still have the extra interpreter overhead but for 200k entries you're surely better off popping. Or I guess the resize part is still O(N) but the conditionals are all being checked twice I would also assume that you're slowing down because you're exceeding a memory limitation, that's simply a lot of data for 1 machine QuarkJets fucked around with this message at 21:48 on Jun 17, 2022 |
# ? Jun 17, 2022 21:45 |
|
Good advice above, adding: doing experimentation like this in jupyter notebooks is my fav. You can just throw a %time at the top of the cell and see how long it took versus the other way you were considering. Just write prototype code for each, then delete the slow one. Once the cells work in sequence, make it a .py file. IMO this method of developing poo poo is way better at catching gotchas/slow code/errors than writing things in a .py file and testing them over and over as a monolith. ...though doesnt work well for things like django/flask/fastapi webapps.
|
# ? Jun 17, 2022 22:06 |
|
CarForumPoster posted:Good advice above, adding: doing experimentation like this in jupyter notebooks is my fav. You can just throw a %time at the top of the cell and see how long it took versus the other way you were considering. Just write prototype code for each, then delete the slow one. Once the cells work in sequence, make it a .py file. Yeah, and remember that VSCode basically works with Jupyter notebooks out of the box if you don't want to go through the whole install process/etc. It's very convenient for any data munching work.
|
# ? Jun 17, 2022 22:23 |
|
It's worth pointing out that memray exists and is good for figuring out if you have memory issues, and can trace native code. https://github.com/bloomberg/memray
|
# ? Jun 17, 2022 23:03 |
|
I'm going through the Codecademy python course and there were some exercises combining functions and list stuff that kind of stymied me. I admittedly had to look up the solution, but I was at least able to solve some of it on my own. Here's the gist of the exercise: quote:For the final code challenge, we are going to create a function that finds the middle item from a list of values. This will be different depending on whether there are an odd or even number of values. In the case of an odd number of elements, we want this function to return the exact middle value. If there is an even number of elements, it returns the average of the middle two elements. Here is what we need to do: Furthermore, quote:Create a function called middle_element that has one parameter named lst. Here's the solution: code:
And as an added bonus: I sometimes get the basic idea of how to solve something, but the specific syntaxes or rules get jumbled in my head. For example, in another definition I had a function that didn't work because I was using sort() instead of sorted and the former doesn't return a value while the latter does. Or there was one exercise where I had to take an existing list and make a new list with it but with the middle item removed, and I couldn't figure it out. Is there a way to get that kind of thing organized in my head?
|
# ? Jun 19, 2022 04:32 |
|
Seventh Arrow posted:My questions: 1. You can verify this for yourself, by opening a Python terminal and running "type(4/2)". In my terminal, this returns a float. To get an int you'd use // instead of / 2. It's grabbing the middle element. I'm not sure what you mean by "is it just rounding the result up to three". If there's an odd number of elements, then the index of the center element is floor(len(x) / 2). Since it's odd-length, the length divided by two leaves with with a remainder; using int() truncates that remainder. If the list length is 3, you want index 1. 3. Square brackets are only used to access entries in a list If you're not 100% certain about how something works, then you should use the internet to look up that information, or in a python terminal you can run "help(sorted)" or "help(list.sort)". Or you can write out a little toy script that tests some specific functionality that you're not sure about, if it's not as simple as "hey uhh I forgot exactly what this function does". I've been writing Python code for 20 years and I still use help on basic functions that I haven't used in awhile.
|
# ? Jun 19, 2022 05:04 |
|
The square brackets are what python calls a "subscription". It's from a math thing that I can't fully explain but is probably available on the internet. The main implication, IMHO, is that the item in the subscript is a member of (exists in) the container. Under the hood, when you call "[1]" on some python thing, you're (usually) calling thing.__getitem__(1) Python code:
|
# ? Jun 19, 2022 05:23 |
|
Thanks for the help! That gives me a lot to mull over.
|
# ? Jun 19, 2022 06:44 |
|
QuarkJets posted:If you're not 100% certain about how something works, then you should use the internet to look up that information, or in a python terminal you can run "help(sorted)" or "help(list.sort)". Or you can write out a little toy script that tests some specific functionality that you're not sure about, if it's not as simple as "hey uhh I forgot exactly what this function does". I've been writing Python code for 20 years and I still use help on basic functions that I haven't used in awhile. I keep an ipython interactive interpreter window open most of the time I'm coding for just this purpose. "Wait, can I add two dictionaries together with +? Let me find out...yep!" etc.
|
# ? Jun 19, 2022 19:47 |
Yeah definitely. And in case anybody doesn't know, one of the things that makes Django development such a joy is that you can ./manage.py shell and it will let you tinker arbitrarily with Python within the context of your webapp, do database operations, import and run methods, all kinds of stuff. And if you have ipython installed, it automatically invokes that instead of your regular REPL, so you get CLI history, command completion, all the cool features.
|
|
# ? Jun 19, 2022 20:48 |
|
Dawncloack posted:I am saved! I am a born-again databaser now! Then we should say the sinners prayer. Oh Postgres, I I've sinned and used MySQL. I promise I will learn about Normalization. I promise I will understand how triggers , functions and views work. I promise I will bake the bosses noodle by creating a custom foreign data wrapper that can drive the office coffee machine using database inserts. I promise I will use JSON fields sparingly. I promise I will learn to index wisely. I promise I will learn and embrace the gospel of EXPLAIN and EXPLAIN ANALYSE. I promise I will learn to partit...wait you mean ..... loving non-ordinal key?.... gently caress. HAIL SATAN.
|
# ? Jun 24, 2022 09:50 |
|
Another Codecademy woe. This time I don't feel so bad because I'm pretty sure they haven't covered this before. Anyways, The lesson is teaching about iterating over strings, and here is the challenge: quote:Write a function called common_letters that takes two arguments, string_one and string_two and then returns a list with all of the letters they have in common. I'm just going to go ahead and post the solution: code:
code:
Also, what is going on in code:
|
# ? Jun 24, 2022 22:47 |
|
Alternate solution use sets and intersection of the two lists
|
# ? Jun 24, 2022 22:55 |
|
When you index a string, you use an integer to pull out the nth character (starting from zero):Python code:
Python code:
The parentheses in that conditional are doing nothing, they are just an attempt to make it easier for you, a human, to separate out the conditions being tested from the syntax of an if statement.
|
# ? Jun 24, 2022 23:05 |
|
Phone posting but you should try "for l in string_one, print l". The type error I imagine is because you're using a letter as a list index, but list indexes should only be integers. e: poster above me said it much better
|
# ? Jun 24, 2022 23:05 |
|
Dunno how much this will help but if you throw the below example into python (an online compiler can be helpful for simple stuff) and run it, it might help you visualise what's happening when you try to loop through the index and values of an array (edit: in the way that you've tried! Other people are very correct in saying that if you need to iterate through a list, just do it, you don't need the index for what you're doing. This post is because it looked like you might be struggling with arrays and indices in general, for eg if you've tried dabbling in JavaScript where it's a lot more common in beginner courses.. or at least it used to be) Python code:
Python code:
Python code:
Python code:
boofhead fucked around with this message at 00:33 on Jun 25, 2022 |
# ? Jun 25, 2022 00:13 |
|
Seventh Arrow posted:Which just gave me type errors. Is that kind of iteration only for integers? I've seen before that you can slice strings with square brackets, so I'm not sure what the problem is (I'm not even sure what I wrote would work with integers, the continual use of the "l" seems off). That kind of iteration is valid for any kind of iterable. A string is an iterable of characters. When you iterate over a string, you're getting characters back. Accessing elements in an iterable with square brackets (slicing or not) requires an integer If you wanted to iterate over indices, you'd have to do something else such as using range() to iterate over the string's length. Check out the outputs of these loops: Python code:
Python code:
Python code:
I'm going to present some things that I think are cool but probably go beyond your lesson plan, feel free to ignore them. Here's a set comprehension: Python code:
Python code:
|
# ? Jun 25, 2022 00:23 |
|
Sad Panda posted:Alternate solution use sets and intersection of the two lists Usually their exercises don't go beyond what the course has taught up until that point, though. However, I will look up sets because it sounds intriguing! Thanks! ArcticZombie posted:
I thought that "l" if you're doing "for l in string_one..." the l is treated as a kind of temporary variable and represents each individual character in string_one. Or am I way off? Thank you for letting me know about the parentheses, this is kind of what I suspected. 12 rats tied together posted:Phone posting but you should try "for l in string_one, print l". The type error I imagine is because you're using a letter as a list index, but list indexes should only be integers. Oh! I think I see. So when you're using a capital "L" list, you should iterate with integers. But with strings, not so (even if you can slice them with, say, [4: ]). Am I correct?
|
# ? Jun 25, 2022 00:25 |
|
QuarkJets, that makes a lot of sense, thank you!
|
# ? Jun 25, 2022 00:28 |
|
Seventh Arrow posted:Oh! I think I see. So when you're using a capital "L" list, you should iterate with integers. But with strings, not so (even if you can slice them with, say, [4: ]). Am I correct? My apologies, I misspoke by calling the string "a list", QuarkJets used the correct terminology by calling them "an iterable". The relationship both string and list have with indexes is that you can access elements in them with an integer or a slice (which is a shorthand for a specific range of integers). The thing I was hoping to have you notice (others have done a better job), is that it doesn't really make sense linguistically to pull the "Dth" or "Drd" element out of a list of numbered somethings. There are other types of data structures where this might make sense, but not the list or the string.
|
# ? Jun 25, 2022 02:32 |
|
|
# ? May 15, 2024 15:56 |
|
Yo Seventh Arrow check this outPython code:
Not very helpful here but I thought you might be interested in seeing what enumerate does
|
# ? Jun 25, 2022 03:19 |