|
code:
code:
code:
code:
|
# ? Oct 13, 2015 09:31 |
|
|
# ? May 9, 2024 02:22 |
|
Popular Thug Drink posted:i'm trying to make a palindrome tester, what am i doing wrong here? The "return True" is over indented and inside the while loop, so your code will either return True or False on the first iteration (see Hammerite's analysis). You should be good if you remove an indent and have "return True" at the same level as the while loop. Xeno54 fucked around with this message at 14:25 on Oct 13, 2015 |
# ? Oct 13, 2015 14:20 |
|
Xeno54 posted:The "return True" is over indented and inside the while loop, so your code will either return True or False on the first iteration (see Hammerite's analysis). You should be good if you remove an indent and have "return True" at the same level as the while loop. i thought i tried that, huh. so if i do that, then 'while the counter is whatever if comparison is still valid deincrement searchLength and try again' is the expected behavior? e: haha, that was the correct placement of the return in my previous test, but i forgot to test for str[index] == str[index-1]], and one of my test words was 'banana', which is a valid offset palindrome under that logic... boner confessor fucked around with this message at 15:57 on Oct 13, 2015 |
# ? Oct 13, 2015 15:47 |
|
Perhaps you want some more practice with loops, but, reminder: a palindrome is a word that is the same forwards and backwards.
|
# ? Oct 13, 2015 16:01 |
|
SurgicalOntologist posted:This my first foray into scraping Javascript pages, and I'm trying to use dryscrape which is based on QT webkit. If everything is working correctly I should be triggering a "download file" popup. How do I catch the file the site is trying to send me, ideally into a string but I would be happy reading it again off the local file system. is your download being triggered by redirecting you to another page? if it is, you can extract the url from the previous page with dryscape and parse the download link out of that, then use python's HTTP library to load the binary data into a string, such as this: code:
|
# ? Oct 13, 2015 17:40 |
|
Popular Thug Drink posted:i'm trying to make a palindrome tester, what am i doing wrong here? Here is what I would consider a more Pythonic palindrome tester (not vigorously tested but seems to work); that is, it uses a comprehension (use comprehensions everywhere). This is Python 3 code, but will work in Python 2 if you remove the annotations from the method signature and use def is_palindrome(palindrome): instead: Python code:
The way this works is as follows:
It is surprisingly common to be able to distill logic into a comprehension in Python, and it's a really valuable pattern to use; smart use of comprehensions is one of the best things you can do for keeping things concise in Python, since the rest of the language can require some extra verbosity that other languages don't. Importantly, it also forces your logic into a single line that reads pretty naturally with a little experience: you can read the comprehension as "this will return a list of boolean values that say whether the character at positions i and the mirror of i were the same while comparing characters in the string through the first half of the string".
|
# ? Oct 14, 2015 04:03 |
|
I can't resist, even though Suspicious Dish already hinted at it in a much more pedagogical manner. But here's my solution:Python code:
Nitrocat posted:is your download being triggered by redirecting you to another page? if it is, you can extract the url from the previous page with dryscape and parse the download link out of that, then use python's HTTP library to load the binary data into a string, such as this: I used Chrome dev tools to find the POST that generates the data, but it includes a whole lot of json. I tried copy-pasting it from Chrome and keep getting JavaScript errors. I still need to try copy-pasting the headers as well. But I'm wondering if it would be possible to use dryscrape or a similar tool to get the POST payload that gets sent when I click the "download" button. Then I send that payload through requests.
|
# ? Oct 14, 2015 04:57 |
|
SurgicalOntologist posted:I can't resist, even though Suspicious Dish already hinted at it in a much more pedagogical manner. But here's my solution: Python code:
|
# ? Oct 14, 2015 06:19 |
|
Suspicious Dish posted:
|
# ? Oct 14, 2015 06:34 |
|
In matplotlib, I want to create axes at arbitrary positions who share size and yaxis/xaxis with some other axis. That is, I have created an axis, and now I want to create another axis at another position with the same extent, but different origin. I do not see an elegant way of doing this. Any tips?
|
# ? Oct 14, 2015 13:04 |
|
SurgicalOntologist posted:I used Chrome dev tools to find the POST that generates the data, but it includes a whole lot of json. I tried copy-pasting it from Chrome and keep getting JavaScript errors. I still need to try copy-pasting the headers as well. But I'm wondering if it would be possible to use dryscrape or a similar tool to get the POST payload that gets sent when I click the "download" button. Then I send that payload through requests. In the Netowrk tab of the Chrome dev tools, you can copy the request as a cURL command which will include the headers. You can also just copy the headers to see what's being sent and incorporate that into a urllib request.
|
# ? Oct 14, 2015 14:29 |
|
Cingulate posted:In matplotlib, I want to create axes at arbitrary positions who share size and yaxis/xaxis with some other axis. Do you have an example of the kind of figure you are trying to create? My guess of the way to approach this is to create another subplot with the same axis size, and the use subplot2grid() to move it where you want.
|
# ? Oct 14, 2015 18:59 |
|
Im not sure I get virtualenv. I tried to install Flask on my Mac per the instructions, but the default is to run 2.7.10 whereas I'm using 3.2. Anyone know how to fix this?
|
# ? Oct 14, 2015 19:00 |
|
BannedNewbie posted:Do you have an example of the kind of figure you are trying to create?
|
# ? Oct 14, 2015 20:12 |
|
spiralbrain posted:Im not sure I get virtualenv. I tried to install Flask on my Mac per the instructions, but the default is to run 2.7.10 whereas I'm using 3.2. Anyone know how to fix this? You will have installed virtualenv as a package of some Python. If you then use virtualenv to create a virtual environment, by default it will create a venv for the Python that it is installed to. Python 2.7.10 is the system Python for new versions of OSX, so I am guessing you installed virtualenv to that Python instead of your 3.2 (simply installing 3.2 will not overwrite the system Python and the system Python remains the default unless you specifically change your PATH variable). You can still use this virtualenv with Python 3.3 by specifying which interpreter you want when you create the venv, something like virtualenv -p /usr/local/bin/python3 mypy32env.
|
# ? Oct 14, 2015 20:23 |
|
Cingulate posted:The problem is mainly, how do I know the first axes' size? Again, it would be easier to help you out if I had a clear understanding of what you're trying to do, but something like this might work: code:
|
# ? Oct 14, 2015 21:52 |
|
Suspicious Dish posted:
the magnificent burn of being ultra code golfed
|
# ? Oct 14, 2015 23:31 |
|
That isn't code golf at all. That is indeed the most straightforward way to test a palindrome in any language. 1. Create a reversed copy of the original string. 2. Compare the copy to the original, testing for equality. It just so happens that the idiomatic way to reverse a string in Python is short and somewhat cryptic.
|
# ? Oct 15, 2015 02:03 |
|
OnceIWasAnOstrich posted:You will have installed virtualenv as a package of some Python. If you then use virtualenv to create a virtual environment, by default it will create a venv for the Python that it is installed to. Python 2.7.10 is the system Python for new versions of OSX, so I am guessing you installed virtualenv to that Python instead of your 3.2 (simply installing 3.2 will not overwrite the system Python and the system Python remains the default unless you specifically change your PATH variable). You can still use this virtualenv with Python 3.3 by specifying which interpreter you want when you create the venv, something like virtualenv -p /usr/local/bin/python3 mypy32env. This helps. Thank you. I want the virtualenv to be used for Python 3.X (forget which version is loaded on my other laptop) so yeah, currently its not pointed at that and I could see it was giving me a path error for the Flask import when I tried running the basic Flask hello world app so I figured something in the virtualenv wasn't setup correctly. Thanks for that command. I'm still not exactly sure how the virtual workspace works but it seems as though it makes it easier to switch between 2.7 and 3.x depending on which version you want to work with...also something with dependencies, but thats about all I know. Def need to read up more on this.
|
# ? Oct 15, 2015 03:10 |
|
spiralbrain posted:This helps. Thank you. I want the virtualenv to be used for Python 3.X (forget which version is loaded on my other laptop) so yeah, currently its not pointed at that and I could see it was giving me a path error for the Flask import when I tried running the basic Flask hello world app so I figured something in the virtualenv wasn't setup correctly. Thanks for that command. You may want to look into Anaconda Python. I really like conda for creating environments. code:
Another nice thing is I can point PyCharm to the environment directory, and it will take it from there automatically using the conda environment on a project by project basis.
|
# ? Oct 15, 2015 04:05 |
|
both of these just flip the string and test that against the input, right? i figured that was more efficient but i wanted to do a step by step comparisonSurgicalOntologist posted:
return is a built in function that produces a bool '' creates a new empty string? .join fills in the new string with the reversed input x == x tests to see if this new string is the same as the input string Python code:
thanks all
|
# ? Oct 15, 2015 06:28 |
|
Popular Thug Drink posted:both of these just flip the string and test that against the input, right? i figured that was more efficient but i wanted to do a step by step comparison return is a keyword that you place inside of a function order to define what that function returns, but return is not a function. In this case the function returns a bool, but a function can return anything. Using == means a comparison is made, and a bool is created from that comparison operation, so returning ''.join(reversed(x)) == x results in a bool getting returned by the function (either True if they are equal, or False if they ar enot) '' is an empty string reversed(x) returns a new string. You don't need to use .join with it. You could just call reversed(x) == x. Joining on an empty string is kind of pointless, really, because strings are immutable: in Python you can never put more or less stuff into a string, you can only create a new string. reverse(x) creates a new string that is the reverse of x .join takes any iterable of strings (say a list of strings, or hey, a string is an iterable of 1-letter strings) and creates a new string out of them, using whatever the string before. join is. In this case, you're using an empty string to join together all of the letters. This is really useful if you want to take a list of strings and separate them with some form of punctuation (commas, or newline characters for instance: printing '\n'.join(['a','test','string'] would result in 3 lines getting printed, one for each word in that list) == x tests to see if the new string is the same as the input string quote:
That's right
|
# ? Oct 15, 2015 11:58 |
|
In Python 3 at least you do need to call join, because reversed returns a special iterator object. join with an empty string essentially concatenates any iterator of strings.
|
# ? Oct 15, 2015 13:34 |
|
Recursively you could do:code:
|
# ? Oct 15, 2015 15:24 |
|
Roargasm posted:Recursively you could do: Pro tip: don't name variables str (or list or int, etc.)
|
# ? Oct 15, 2015 15:53 |
|
Whats the neatest way to sort a list of integers into other lists? At the moment I've got something and its working fine, but I'm sure its probably a really inefficient way to solve this problem.code:
|
# ? Oct 15, 2015 15:56 |
|
vodkat posted:Whats the neatest way to sort a list of integers into other lists? At the moment I've got something and its working fine, but I'm sure its probably a really inefficient way to solve this problem. Something something integer division. x//5M will equal 9 for 45M-49999999, 8 for 40M-44999999, 7 for 35M-39999999 and etc. If you had a list of lists, you could use the 9,8,7,6 to select the proper list.
|
# ? Oct 15, 2015 16:14 |
|
vodkat posted:Whats the neatest way to sort a list of integers into other lists? At the moment I've got something and its working fine, but I'm sure its probably a really inefficient way to solve this problem. My python is super rusty so this may not be very pythonic or even that efficient but code:
e: forgot to append the leftovers, fixed No Safe Word fucked around with this message at 16:23 on Oct 15, 2015 |
# ? Oct 15, 2015 16:19 |
|
vodkat posted:Whats the neatest way to sort a list of integers into other lists? At the moment I've got something and its working fine, but I'm sure its probably a really inefficient way to solve this problem. You should be using numpy. Python code:
|
# ? Oct 15, 2015 16:41 |
|
SurgicalOntologist posted:You should be using numpy. Overkill for something so basic that can easily be done just using Python. Python code:
|
# ? Oct 15, 2015 19:00 |
|
Hammerite posted:Overkill for something so basic that can easily be done just using Python. That's fair, if it's really just this one thing. But someone who can benefit from array indexing once is likely to benefit from it in other places as well.
|
# ? Oct 15, 2015 19:33 |
|
SurgicalOntologist posted:In Python 3 at least you do need to call join, because reversed returns a special iterator object. join with an empty string essentially concatenates any iterator of strings.
|
# ? Oct 15, 2015 19:35 |
|
Hammerite posted:Overkill for something so basic that can easily be done just using Python. Numpy is Python
|
# ? Oct 15, 2015 19:37 |
|
QuarkJets posted:Numpy is Python Probably what he means is "can be done with the Python standard library without needing to install anything".
|
# ? Oct 15, 2015 23:48 |
|
chutwig posted:Probably what he means is "can be done with the Python standard library without needing to install anything". I don't understand, why would you install python without installing numpy?
|
# ? Oct 16, 2015 08:40 |
|
Nippashish posted:I don't understand, why would you install python without installing numpy? Because it's the path of least resistance to just install standard Python, and you don't need to do things that numpy is designed to do (i.e. intensive number-crunching)? I had just python installed on my machines for ages. Recently I installed another Python distribution that has numpy included because I wanted to play around with a neural network library, and I concluded from reading about the amount of matrix maths involved that it would be needed (and besides the library I wanted to use had it as a dependency). If I hadn't been looking at that then I still wouldn't have it installed because day-to-day I don't really use Python to do number-crunching, just to script stuff, and it's fast enough for that purpose.
|
# ? Oct 16, 2015 11:15 |
|
Nippashish posted:I don't understand, why would you install python without installing numpy? Because the vast majority of things people do with Python get no particular benefit out of numpy?
|
# ? Oct 16, 2015 14:49 |
|
Thermopyle posted:Because the vast majority of things people do with Python get no particular benefit out of numpy? This. When I first started using python I had no needs outside of the standard library, but once I started doing my data analysis/cleaning/plotting in python I moved over to the Anaconda distribution since pandas, numpy, and MPL are daily drivers for me now. Once of the things I loved as a beginner to the language was how robust (at least to me) the standard library was in helping me do what I needed.
|
# ? Oct 16, 2015 15:12 |
|
Thermopyle posted:Because the vast majority of things people do with Python get no particular benefit out of numpy? correct. the majority of python users don’t have much of a user for libraries like numpy, matplotlip, panda’s, etc. the standard library is robust enough for what most developers need. i use those libs for data plotting and statistical analysis but for operations that can easily be done in the standard library with a couple lines of code extra, numpy is not needed. why implement a third party lib unnecessarily?
|
# ? Oct 16, 2015 18:40 |
|
|
# ? May 9, 2024 02:22 |
|
I usually agree that numpy is overkill for most general stuff... But man, for this particular case array indexing fits like a glove as SurgicalOntologist showed. So I would've posted this instead: SurgicalOntologist posted:You should be lusting for numpy.
|
# ? Oct 16, 2015 19:58 |