|
code:
|
# ? Aug 2, 2017 05:49 |
|
|
# ? May 16, 2024 10:10 |
|
SurgicalOntologist posted:
Damnit that does work. Thanks for pointing out my idiocy. Suppose I can take the output values and make a new Dataframe with them. What about restricting data to a specific window of days in the year? I know I can slice easily by year-mo (for example, df['1990-01':'1999-12']), but can you slice by mo-dd? It'd be nice to get all the data between, say, June 15 and Sep 30, like df['06-15':'09-30'] but for all years. Is that possible?
|
# ? Aug 2, 2017 08:24 |
|
SirPablo posted:Damnit that does work. Thanks for pointing out my idiocy. Suppose I can take the output values and make a new Dataframe with them.
|
# ? Aug 2, 2017 17:25 |
|
I have a list of lists of words, and a dict of word=>int. For each list, I'm trying to find min(dict[word] for word in list if word in dict). Is there a way to do this in numpy or some other high performance library?
|
# ? Aug 3, 2017 04:20 |
|
I think with pandas you could doPython code:
|
# ? Aug 3, 2017 04:42 |
|
Edit: nevermind.
|
# ? Aug 3, 2017 17:36 |
How do I avoid having import statements below my logging config? My IDE tells me all the import statements should be at the top, but I need to set the logging config before some of these libraries are loaded. This is for a little CLI python app.code:
|
|
# ? Aug 4, 2017 00:26 |
|
fletcher posted:How do I avoid having import statements below my logging config? My IDE tells me all the import statements should be at the top, but I need to set the logging config before some of these libraries are loaded. This is for a little CLI python app. Your IDE doesn't always know where import statements should go. Style guides are just guides.
|
# ? Aug 4, 2017 00:28 |
Thermopyle posted:Your IDE doesn't always know where import statements should go. Style guides are just guides. Ah ok, I figured maybe there was some __init__.py thing I was supposed to be doing or something. Thanks for the tip!
|
|
# ? Aug 4, 2017 00:32 |
|
Say i have a routine that searches for and gets a thing. You can search for a thing via a regex, via an int, or via a tuple. Which of the following do you prefer? one Python code:
Python code:
Python code:
|
# ? Aug 5, 2017 19:00 |
|
One. Otherwise, users of the function will have to do type-checking themselves and call the right function or pass in the right kwargs, when you could do that burdensome work for them and reduce the possibility of error/proliferation of ways of doing said checking. Simple is better than complex.
|
# ? Aug 5, 2017 19:23 |
Thermopyle posted:Say i have a routine that searches for and gets a thing. You can search for a thing via a regex, via an int, or via a tuple. Which of the following do you prefer? Python code:
where predicate is a function/callable which takes as input elements of the space you are searching over and which returns true if a match is found. If you don't want to do that though, I think the first function you posted has the cleanest interface so I would prefer that.
|
|
# ? Aug 5, 2017 19:28 |
|
I don't know why I didn't even think of using a predicate since most of my time is spent doing JS nowadays and thats a very common pattern there. Thanks for reminding me. Now I accept a predicate, or build a predicate for you if you provide the other types I mentioned above.
|
# ? Aug 6, 2017 00:16 |
Thermopyle posted:Say i have a routine that searches for and gets a thing. You can search for a thing via a regex, via an int, or via a tuple. Which of the following do you prefer? I would combine the first two approaches. The first approach is easiest for the user, the second is easiest for the developer. code:
|
|
# ? Aug 6, 2017 03:53 |
Thermopyle posted:I don't know why I didn't even think of using a predicate since most of my time is spent doing JS nowadays and thats a very common pattern there. Thanks for reminding me. Nice, that sounds like an excellent, convenient interface.
|
|
# ? Aug 6, 2017 06:19 |
|
Might be a bit complicated if you're using predicate functions, but singledispatch is an option tooPython code:
|
# ? Aug 6, 2017 16:12 |
|
Well gently caress, how did I not remember single dispatch? I'm happy enough with what I've got going now, but I certainly agree with PEP-443 where they introduce single dispatch where it says: quote:In addition, it is currently a common anti-pattern for Python code to inspect the types of received arguments, in order to decide what to do with the objects. Thats exactly the reason I always get the heebie-jeebies when I see a function body starting out with a bunch of isinstance checks. Thermopyle fucked around with this message at 19:02 on Aug 6, 2017 |
# ? Aug 6, 2017 18:59 |
|
There's another pattern I've seen (forget the name) where you have a list of handlers and you just fire your arguments at them until one of them goes 'yeah I've got this'. That way all the type checking goes on in the handler classes and only for the types they handle, and adding more cases just means writing a handler and adding it to your list Feels a bit Java but I saw it on Python examples sooo
|
# ? Aug 6, 2017 19:14 |
I completely forgot about singledispatch. I don't think I've ever used it, but maybe I should have.
|
|
# ? Aug 6, 2017 21:15 |
|
A Flask question, maybe just a style one. I'm tidying up a Flask web-service and was just going to stick in the footer: - The current number of records ("Currently storing 323,000 foos!") - A link to the latest news item (which are also stored in the flask db) But it's not kosher to stick this sort of logic in footer template. And it's another two queries for each page. And there's no need to call (and recall) these two queries for every page, as they don't have to be precisely up to date. So what's the neatest way to do this?
|
# ? Aug 7, 2017 15:34 |
|
outlier posted:A Flask question, maybe just a style one. Cache the database results somewhere (memcache). Update the cached values when new news item posted and every X period of time depending on how accurate you want the number of records to be.
|
# ? Aug 7, 2017 16:02 |
|
Eela6 posted:I completely forgot about singledispatch. I don't think I've ever used it, but maybe I should have. I feel like I need to review functools to re-learn all of it's cool features.
|
# ? Aug 7, 2017 16:05 |
|
outlier posted:A Flask question, maybe just a style one. Like Thermopyle said, you want to use a cache for this. Depending on what kind of traffic your Flask app will see, you can either use memcached or Werkzeug has a built in SimpleCache object.
|
# ? Aug 7, 2017 16:17 |
|
Tigren posted:Like Thermopyle said, you want to use a cache for this. Depending on what kind of traffic your Flask app will see, you can either use memcached or Werkzeug has a built in SimpleCache object. Noice. Thanks both.
|
# ? Aug 7, 2017 17:25 |
|
i've started playing around with python for work mostly so i can make charts that can be tweeted out quickly or whatever. All i really want to do is import csv's so i can start using matplotlib stuff with them but finding an easy guide of doing this is really annoying me. I've spent a while playing with anaconda and i can import the csv using a couple of methods but they've all added stuff to the output. I'm just after either a decent guide using unicodecsv which seems to have replaced the csv package or the short bit of code that will get me an array or whatever where the first row of the csv is saved as column headers so I can stick things onto an x and y axis. I only started doing the data camp courses that were free on friday so far
|
# ? Aug 14, 2017 15:37 |
|
Jose posted:i've started playing around with python for work mostly so i can make charts that can be tweeted out quickly or whatever. All i really want to do is import csv's so i can start using matplotlib stuff with them but finding an easy guide of doing this is really annoying me. I've spent a while playing with anaconda and i can import the csv using a couple of methods but they've all added stuff to the output. I'm just after either a decent guide using unicodecsv which seems to have replaced the csv package or the short bit of code that will get me an array or whatever where the first row of the csv is saved as column headers so I can stick things onto an x and y axis. You really want csv.DictReader out of the standard library for this. Unless there's unicode in your CSVs in which case unicodecsv should have a DictReader equivalent. p
|
# ? Aug 14, 2017 15:55 |
|
If it's tabular data, just use pandas read_csv (and make plots in seaboard).
|
# ? Aug 14, 2017 16:08 |
Jose posted:i've started playing around with python for work mostly so i can make charts that can be tweeted out quickly or whatever. All i really want to do is import csv's so i can start using matplotlib stuff with them but finding an easy guide of doing this is really annoying me. I've spent a while playing with anaconda and i can import the csv using a couple of methods but they've all added stuff to the output. I'm just after either a decent guide using unicodecsv which seems to have replaced the csv package or the short bit of code that will get me an array or whatever where the first row of the csv is saved as column headers so I can stick things onto an x and y axis. The csv module in the python standard library will work just fine for you. Here's an example to get you started. Suppose we have a CSV of wedding guests, that contain their name, their relationship to the bride and groom, and their food preferences. Here's our CSV: code:
We want to process the list to find out which guests need what food. We can do so as follows: IN Python code:
code:
|
|
# ? Aug 14, 2017 17:39 |
|
Cingulate posted:If it's tabular data, just use pandas read_csv (and make plots in seaboard). Seriously, do this. Do not even consider the csv reader in the stdlib. Pandas is infinitely superior.
|
# ? Aug 14, 2017 19:53 |
|
I don't think you need to drag along a bunch of fortran libraries (via numpy and scipy) to parse csv files.
|
# ? Aug 14, 2017 19:57 |
|
You should though, especially since the next step is "make charts".
|
# ? Aug 14, 2017 19:58 |
|
Situations where I can see the hand-crafted option make sense: - you're analysing data and tweeting on an embedded system running python 2.6 on 128MB RAM - you want to Learn Python the Hard and Manly Way Situations where I'd prefer the Pandas option: - you want to get things done and learn the tools you're actually gonna use in real situations Anything I forgot? Jose, in case "seaboard" confused you, I apologise: install anaconda and go here.
|
# ? Aug 14, 2017 20:11 |
Nippashish posted:Seriously, do this. Do not even consider the csv reader in the stdlib. Pandas is infinitely superior. I don't feel that way. I don't particularly like pandas, and I prefer using the stdlib where possible. You have to consider the audience of your code, too. Jose is very new to python; the last thing he needs is a thousand different APIs to understand. Cingulate posted:Situations where I can see the hand-crafted option make sense: I am no fan of masochism. I just don't like solutions that are 'well, first install this dependency' for simple problems. I think you should use the level of tool that's appropriate for your problem. If I'm using CSVs, I'm probably going to be doing it by hand. If I have a big dataset that requires the big guns, 1. I'm going to use xarray , not pandas 2. Why the hell am I using CSVs? Eela6 fucked around with this message at 20:24 on Aug 14, 2017 |
|
# ? Aug 14, 2017 20:19 |
|
Eela6 posted:I don't feel that way. I don't particularly like pandas, and I prefer using the stdlib where possible. You have to consider the audience of your code, too. Jose is very new to python; the last thing he needs is a thousand different APIs to understand.
|
# ? Aug 14, 2017 20:24 |
|
Eela6 posted:I don't feel that way. I don't particularly like pandas, and I prefer using the stdlib where possible. You have to consider the audience of your code, too. Jose is very new to python; the last thing he needs is a thousand different APIs to understand. The audience is someone who wants to load data from csvs to produce charts. Someone who is already using matplotlib and is enrolled in something called "data camp". Pandas is a cornerstone of the python data science toolchain. Avoiding it is pretty much the data science equivalent of rolling your own web server.
|
# ? Aug 14, 2017 20:34 |
|
Probably, most people who need to read csv's don't need pandas, but it certainly seems like this person could use it.
|
# ? Aug 14, 2017 20:38 |
|
I also bet he's going to need to do manipulation/cleaning of the data before plotting which will be much easier in pandas, but sure reinvent the wheel.
|
# ? Aug 14, 2017 20:57 |
|
Eela6 posted:I don't feel that way. I don't particularly like pandas, and I prefer using the stdlib where possible. You have to consider the audience of your code, too. Jose is very new to python; the last thing he needs is a thousand different APIs to understand. (You're probably using CSVs/pandas because 1. the party/platform you got the data from uses CSVs/stuff pandas can easily parse.) I don't think this is about "big guns". It's just so much more handy to use pandas and the standard scientific Python stack even for tiny data. It seems like 1/3rd of questions ITT are solved by SurgicalOntologist posting a pandas one-liner.
|
# ? Aug 14, 2017 22:05 |
Thermopyle posted:Probably, most people who need to read csv's don't need pandas, but it certainly seems like this person could use it. Fair enough.
|
|
# ? Aug 14, 2017 22:16 |
|
|
# ? May 16, 2024 10:10 |
|
Speaking of Pandas, I run in to trouble when I need to create additional columns that are filled based on other column criteria. For example, if I have a CSV of:code:
How would I do something like that?
|
# ? Aug 15, 2017 02:46 |