|
Thermopyle posted:Here's a neat thing if you're one of the cool people like me who can mostly move most of their projects to keep up with the latest python versions. Coming in Python 3.7 are data classes! There's already a backport to 3.6. The __post_init__ method is a nice addition too, if you want to do more in-place data wrangling. https://pypi.python.org/pypi/dataclasses/
|
# ? Jan 25, 2018 22:09 |
|
|
# ? May 15, 2024 03:52 |
|
ZarathustraFollower posted:filename=('HeteroFreq2.txt'), That comma one the end makes it a tuple with one element. Delete the comma (and maybe also the parens because they're not doing anything).
|
# ? Jan 25, 2018 22:15 |
|
Thermopyle posted:Here's a neat thing if you're one of the cool people like me who can mostly move most of their projects to keep up with the latest python versions. Coming in Python 3.7 are data classes! Say I have a program that uses a dictionary to pass data between functions. But at this point it's getting unwieldy, and attribute access sounds more appealing. Is there something in PyCharm that makes that refactoring less painful?
|
# ? Jan 25, 2018 22:41 |
|
Dr Subterfuge posted:Say I have a program that uses a dictionary to pass data between functions. But at this point it's getting unwieldy, and attribute access sounds more appealing. Is there something in PyCharm that makes that refactoring less painful? I can't think of anything specific to PyCharm that would make that easier other than maybe some fancy regex search/replace. You can write a class and override the appropriate dunder methods for attribute access...or you can use a library like Box which allows you to access your dict via attribute or item access. That would allow you to keep your functions as is and slowly convert them over to dotted access. That being said, I'm not convinced it's a good idea. On the one hand, I also find dotted access smoother and less irritating to use, but on the other hand the way you access dicts and the way you access objects is an established thing in Python-land. Whatever you do, I don't think there's an easy way to refactor this. I've done the method where I implement the appropriate dunder methods on a class to let me access a dict via attribute and the bad part when it comes to PyCharm is that you don't get any help on autocompletion since the attributes are dynamic in nature.
|
# ? Jan 25, 2018 22:55 |
|
So I made the needed adjustments to my folium script and I still wasn't seeing the map appear in jupyter. But apparently this is just down to jupyter being dumb sometimes, so someone on the folium message boards suggested something called "nbviewer", and now it finally works: http://nbviewer.jupyter.org/gist/ocefpaf/a9afd09bea76bda67b5bf3e0961c30f5 edit: still using iterrows because I think I'm too dense to figure out the alternatives
|
# ? Jan 25, 2018 23:25 |
|
So a friend of mine is about to start a Python programming job, and he asked me if I had any Python beginner resources. I recommended Fluent Python and Think Python, but I'm not sure if there's more than that that's highly recommended. Fake edit: Nevermind, I found this huge list in the Python website: https://wiki.python.org/moin/BeginnersGuide/Programmers Space Kablooey fucked around with this message at 03:22 on Jan 26, 2018 |
# ? Jan 26, 2018 03:19 |
|
So I've got one I've never had to deal with before that I'm really struggling to google or even figure out. I'm working on writing a slackbot and I'm printing out a list of usernames. One of my users has a Hebrew name. code:
e: Python 2.7, unicode is a trash fire. I printed the output to the terminal. Here are screenshots of how Slack and iterm2 parse the string, as well as the raw output. u'1: \u03bb - 0.2712889rc\n2: \u03b1 - 0.16032rc\n3: \u03a9 - 0.12084rc\n4: lamellafella - 0.11516rc\n5: jambonn\xe9 - 0.10515rc\n6: sean - 0.10371rc\n7: \u05d2\u05d5\u05dc\u05dd - 0.06084rc\n8: heidegrrrl - 0.06046rc\n9: nera - 0.0318611rc\n10: hav\xe6\xf1t - 0.02973rc\n' Taken as raw symbolic order, the Hebrew is not in the correct order in the string. \u05d2\u05d5\u05dc\u05dd if translated in place would be the Hebrew word but in reverse. I guess Slack is correctly reversing it, but iterm is not. The question, though, is why is it swapping the position of the name and the number... The March Hare fucked around with this message at 04:03 on Jan 26, 2018 |
# ? Jan 26, 2018 03:43 |
|
Stick a \u200E in it https://www.w3.org/International/questions/qa-bidi-unicode-controls
|
# ? Jan 26, 2018 04:04 |
|
tef posted:Stick a \u200E in it I'm not sure I would have ever figured this out, thank you.
|
# ? Jan 26, 2018 04:09 |
|
What's messing up is the bidi algorithm in the big table of stuff http://unicode.org/reports/tr9/#Table_Bidirectional_Character_Types you can see that numbers are weak, so they're influenced by the text surrounding them. in this case, some implementations see it as part of the hebrew text because it's just numbers, resetting when the encounter a dollar sign, others recognize it as belonging to the dollar sign, or surrouding letters
|
# ? Jan 26, 2018 04:14 |
|
tef posted:What's messing up is the bidi algorithm Yeah, I just tossed the hard ltr char right after the name and it looks fine now. Thanks~
|
# ? Jan 26, 2018 04:16 |
|
Thermopyle posted:I can't think of anything specific to PyCharm that would make that easier other than maybe some fancy regex search/replace. Well you've given me the idea of just running a regex script on the files themselves that subs out a bracketed key in the code for the dotted equivalent. Shouldn't be too hard to make a pattern for, at least for the majority of cases. Hopefully. At least it gives me an excuse to mess with functional replacement in re.sub. Thanks!
|
# ? Jan 26, 2018 04:50 |
|
The March Hare posted:Yeah, I just tossed the hard ltr char right after the name and it looks fine now. Thanks~ code:
|
# ? Jan 26, 2018 06:00 |
|
I have another potentially stupid question that I can hopefully explain in something resembling a comprehensible fashion: Let's say I have a paragraph of text with hard line breaks in a list, with each line as an item: code:
code:
code:
code:
|
# ? Jan 26, 2018 14:43 |
|
Wallet posted:I have another potentially stupid question that I can hopefully explain in something resembling a comprehensible fashion: You could generate a single list of all the numbers, then iterate through the original lines, and for each word on each of the original lines, pop a number from the single list to attach to the end of the word. Edit: Something along these lines: Python code:
Jose Cuervo fucked around with this message at 15:01 on Jan 26, 2018 |
# ? Jan 26, 2018 14:57 |
|
The easiest thing would be to add the numbers as you do the counting - I'm guessing you're splitting the sentence on whitespace, and doing something smart about punctuation, so that would be a good time to decide where the count should go (like it needs to go before that colon in the first sentence, but apostrophes would be treated differently etc). So you'd take each word, count it, then transform the string to add the count, and eventually just use join to put it all back together If you're in the situation where the counts have already been done, you just need to get back to that state again - split each sentence into words. Then you'll have two structures that mirror each other (a list of lists of words, and a list of lists of counts), and you can iterate over those together, using zip to get pairs of word lists and count lists, and using zip again on those to get pairs of words and counts tldr you're probably looking for zip
|
# ? Jan 26, 2018 15:00 |
|
Thanks for the responses guys, that's more or less what I was thinking but I wasn't sure if I was missing a super clever approach. I will have to look into zip, however, as I wasn't aware of it and it seems extremely useful.
|
# ? Jan 26, 2018 18:07 |
|
Is this a place for stupid newbie Python questions? I am working on a class assignment where I have to use a dynamic programming implementation of the make change problem. The algorithm is fine if I hard code values into it, but I'm having a hard time reading data from the required text file and using that data. Input .txt file looks like this: code:
code:
code:
code:
code:
|
# ? Jan 28, 2018 20:56 |
|
If you look at the error, it's saying you can't do str() <= int(), and there's something that looks like that in the line it quoted you're casting to int after you've done the comparison - you need to get used to the order it's written in python, but it does the for bit to get items, then the if bit to filter them, and then it does the bit on the left with the results. You can think of the for part as item from items (where item passes this test)
|
# ? Jan 28, 2018 21:44 |
|
baka kaba posted:If you look at the error, it's saying you can't do str() <= int(), and there's something that looks like that in the line it quoted Got it working, thanks for your explanation on the for/if!
|
# ? Jan 28, 2018 21:59 |
|
Is there any reason you're storing the data you're reading in as strings instead of integers?
|
# ? Jan 28, 2018 22:02 |
|
Yeah ideally you'd convert all that as you parse it. Read in each line, split it and convert each item to an int. That way your "get input" part hands you a pristine set of int lists, which is the structure you want to work with
|
# ? Jan 29, 2018 00:10 |
|
I'm working on converting a sequence of calculations done in an Excel workbook into a python script so we can automate the process. I've been writing tests for each function where the function represents an algorithm for one computation (interpolation, some PDEs, etc...) typically with an input of a few lists and an output of a new list. The issue I'm having is that I keep getting varying amounts of precision loss throughout the process when comparing my results to the output results of the Excel computations. I'm working with values that need to be precise out to about 6 decimal places, but python seems to handle floating points somewhat arbitrarily? For example I load in a list of values that look like 0, .25, .5, 1, 1.25, 1.5, etc... in Excel and my resulting python list is 0, .25, .4999999999999, 1, 1.249999999999993, 1.5, etc... I know floating point math is a nontrivial issue, but is there a standard way to handle this stuff when doing routine calculations on numbers that are very precise? My current thought is to just change my tests to pass if there is less than n% difference between the Excel values and the python computed values, but that seems a little hand wavy.
|
# ? Jan 29, 2018 00:30 |
If you want arbitrary precision, you should use the decimal library. Alternately, use math.isclose()
|
|
# ? Jan 29, 2018 00:37 |
|
Portland Sucks posted:I know floating point math is a nontrivial issue, but Welcome to the wonderful world of floating point: https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats https://docs.python.org/3.7/tutorial/floatingpoint.html But you probably want, https://www.python.org/dev/peps/pep-0485/ https://docs.python.org/3/library/math.html see isclose()
|
# ? Jan 29, 2018 00:38 |
|
Portland Sucks posted:I know floating point math is a nontrivial issue, but is there a standard way to handle this stuff when doing routine calculations on numbers that are very precise? My current thought is to just change my tests to pass if there is less than n% difference between the Excel values and the python computed values, but that seems a little hand wavy. You're opening a huge can of worms here by asking about precision, although almost certainly the answer you want is "learn to live with small amounts of precision difference". However, if you're seeing differences as early as just reading the values and looking at what you read it is extremely likely that excel actually has the same precision loss as python and is just rounding differently for display purposes.
|
# ? Jan 29, 2018 00:40 |
|
pmchem posted:But you probably want, Got it, I guess this is pretty much how I was attempting to write my tests. I figured that Excel likely had precision loss in it as well and I know the guy who originally wrote the Excel workbook we're using put no thought into what he was doing regarding precision. I just want to make sure that what I'm producing isn't going to be wildly off the mark.
|
# ? Jan 29, 2018 00:56 |
|
Portland Sucks posted:Got it, I guess this is pretty much how I was attempting to write my tests. I figured that Excel likely had precision loss in it as well and I know the guy who originally wrote the Excel workbook we're using put no thought into what he was doing regarding precision. I just want to make sure that what I'm producing isn't going to be wildly off the mark. Also take a look at numpy.testing.assert_allclose for testing all values in an array.
|
# ? Jan 29, 2018 05:06 |
|
e: misread
Boris Galerkin fucked around with this message at 09:38 on Jan 30, 2018 |
# ? Jan 30, 2018 09:34 |
|
Does anyone have experience mirroring PyPi? My work is about to shut off all internet access to internal servers as a security measure, and are working on setting up internal package mirrors for everything we use. I think they're more concerned about the internet connection than malicious packages, which is lucky for me. I'm leaning towards a caching PyPi proxy server like devpi-server rather than an actual mirror of the entire index.
|
# ? Jan 30, 2018 18:17 |
|
Is there a package/library similar to Go's "Cobra"? Command line flagging coffee organizational thing.
|
# ? Jan 30, 2018 21:03 |
|
Hadlock posted:Is there a package/library similar to Go's "Cobra"? Command line flagging coffee organizational thing. I'm not sure you're gonna find something exactly like cobra, but maybe check out Click Python code:
Bash code:
|
# ? Jan 30, 2018 21:10 |
|
When deploying Flask with gunicorn gevent workers do I need to worry about the concurrency of the user session (using the built in module flask.session)? Or is this handled by Flask (Werkzeug) already? Edit: Looks like it doesn't matter; session is saved by the browser so Flask has no 'state' of user sessions to maintain. Based on the answer here: https://stackoverflow.com/questions/38427923/how-does-flask-sessions-work mr_package fucked around with this message at 00:19 on Jan 31, 2018 |
# ? Jan 30, 2018 22:29 |
|
Hadlock posted:Is there a package/library similar to Go's "Cobra"? Command line flagging coffee organizational thing. I personally like docopt, although many prefer click. For docopt, you write the help message first (as a docstring), and docopt parses it. Tigren's example would look something like (modifying it to show positional args and flagged options). Python code:
|
# ? Jan 30, 2018 22:44 |
|
Yeah click looks like exactly what I'm looking for. Docopts looks useful but click, i read the example and that seems very easy to work with. Has anyone ever mixed and matched flask with click? I have a bunch of baby services I'll need to write for a project and my coworkers are still stuck doing cli stuff and haven't graduated fully to api. I thought maybe I could setup the app as a flask api service, but could also be run from the command line if well documented to give my coworkers a chance to come up to speed.
|
# ? Jan 30, 2018 23:18 |
|
Hadlock posted:Yeah click looks like exactly what I'm looking for. Docopts looks useful but click, i read the example and that seems very easy to work with. I've done something similar with Django and argparse. Pretty easy with django management commands.
|
# ? Jan 31, 2018 00:16 |
|
Hadlock posted:Has anyone ever mixed and matched flask with click? I have a bunch of baby services I'll need to write for a project and my coworkers are still stuck doing cli stuff and haven't graduated fully to api. I thought maybe I could setup the app as a flask api service, but could also be run from the command line if well documented to give my coworkers a chance to come up to speed. Click is built into flask already: http://flask.pocoo.org/docs/0.12/cli/ (same author I think)
|
# ? Jan 31, 2018 00:49 |
|
I use Python Fire. quote:You can call Fire on any Python object:
|
# ? Jan 31, 2018 00:52 |
|
porksmash posted:Does anyone have experience mirroring PyPi? My work is about to shut off all internet access to internal servers as a security measure, and are working on setting up internal package mirrors for everything we use. I think they're more concerned about the internet connection than malicious packages, which is lucky for me. I'm leaning towards a caching PyPi proxy server like devpi-server rather than an actual mirror of the entire index. Youve got the right idea. With some of the other methods package searching from pip was wonky, but devpi works pretty well for us.
|
# ? Jan 31, 2018 03:53 |
|
|
# ? May 15, 2024 03:52 |
|
I'm doing something wrong/misunderstanding how to use asyncio. I want to read from stdin, start a task based on that input and be able to receive more input and start more tasks while waiting for that task to complete. But the first task seems to be blocking and I don't understand why. Here is an distilled example (with load simulating a task taking some time):Python code:
code:
code:
code:
|
# ? Jan 31, 2018 15:58 |