|
Within that inner loop, you're appending a card to hand, appending hand to res, and then removing a card from hand. But this modifies the hand that's in res, because you're actually appending a pointer to the list instead of a copy of the list. If you did this instead, it would work as expected: Python code:
Python code:
|
# ? Feb 21, 2015 09:39 |
|
|
# ? May 9, 2024 20:40 |
|
dantheman650 posted:I'm beating my head against the wall about a seemingly simple problem. This is a simple test block for code that will eventually delete a wild card from a poker hand, then create a list of all possible hands (each represented by a list of cards) by substituting each possible card in for the wild card and adding that hand to the list. This is the homework for the Udacity course recommended on the previous page. (I'm loving the course and also CheckIO - thanks for recommending them!) You're appending a reference to the hand object five times so they all end up being identical. Throw a list() around the hand append so it generates a new list. res.append(list(hand)) should work.
|
# ? Feb 21, 2015 09:41 |
|
Thank you both. I can't express how much this thread has been helpful. You are all wonderful!
|
# ? Feb 21, 2015 10:08 |
|
cowofwar posted:Thanks, this seems to work. Although I had to grep out a bunch of lines and cut excess columns because it doesn't tolerate variation. If you're doing operations on tabular data from csv format, the library pandas may also be helpful.
|
# ? Feb 21, 2015 17:46 |
|
I'm pretty new to Python and am trying to scrape a table off a website that's split across multiple pages. This is the address with the page number in bold, going from 1 to 1123. http://www.vdc-sy.info/index.php/en/martyrs/1/c29ydGJ5PWEua2lsbGVkX2RhdGV8c29ydGRpcj1ERVNDfGFwcHJvdmVkPXZpc2libGV8ZXh0cmFkaXNwbGF5PTB8 Here's what I've managed to come up with, just testing the first 10 pages, but it only writes 2 pages worth of data into the csv file and I can't figure out why. Any ideas? code:
Xandu fucked around with this message at 09:02 on Feb 22, 2015 |
# ? Feb 22, 2015 07:22 |
|
code:
so: code:
|
# ? Feb 22, 2015 13:52 |
|
Xandu posted:I'm pretty new to Python and am trying to scrape a table off a website that's split across multiple pages. This is the address with the page number in bold, going from 1 to 1123. Take a look at http://import.io if you'd like a tool to handle this automatically for you.
|
# ? Feb 22, 2015 14:15 |
|
import.io was the first thing I tried, it's really quite cool, but I couldn't quite train it crawl the rest of the pages, though. I've been looking for an excuse to pick up some Python though, anyways.Master_Odin posted:
Ah, this makes sense. I've got it working now, though I'm encountering some weird encoding issue when I try to pull the Arabic version of the site. This pulls the english onto one row per row into the table. code:
code:
code:
|
# ? Feb 22, 2015 20:56 |
Is there a way to check if a variable is a file that works for both Python 2 and Python 3? I have a method that I wanted to be able to accept either a string of a filename or a file like object (like sys.stdin) so I had the following that worked in Python 2: code:
|
|
# ? Feb 23, 2015 22:43 |
|
This doesn't work because open() doesn't return a file object in Python3, and in addition there are many file like objects that are not files, so you shouldn't be checking type identity. There are several options, I'm not sure what would be best. I would probably have two different kwargs, one for a filename or one for a handle. Alternatively you could simply try first and ask for forgiveness: try to open a file and try to use the object as a file handle, a file handle passed to open() should result in a TypeError, while a string won't work as a file like object. There are probably better ways I'm not thinking of.
|
# ? Feb 23, 2015 22:59 |
Thanks OnceIWasAnOstrich. The different kwargs was the route I started going down but I ended up just splitting it into two methods:code:
|
|
# ? Feb 23, 2015 23:06 |
Got another question now...How come Pycharm is giving me warnings with this line?code:
|
|
# ? Feb 23, 2015 23:07 |
|
fletcher posted:It has moves.urllib.parse underlined with "Cannot find references 'moves' in 'six.py'" and urlparse has red squiggly lines "Unresolved reference 'urlparse'" yet everything seems to work just fine when I run it. Do I just have to ignore these IDE warnings when using six? On that note like 90% of every name I hover over, standard library, third-party stuff, my own code, the pop-up documentation just comes back blank though the source has proper docstrings and syntax completion works, so I know it's looking at the right name. Am I doing something wrong or is that feature just weak in general?
|
# ? Feb 23, 2015 23:20 |
|
code:
|
# ? Feb 24, 2015 00:18 |
Fergus Mac Roich posted:
https://docs.python.org/2/tutorial/floatingpoint.html
|
|
# ? Feb 24, 2015 00:23 |
|
Fergus Mac Roich posted:
I'd be curious how you're setting it up in C to be confident in the bolded bit. Here's a python doc on it that might be enlightening, if you really want to learn all the lies that surround FP you could start here.
|
# ? Feb 24, 2015 00:25 |
|
JawnV6 posted:I'd be curious how you're setting it up in C to be confident in the bolded bit. Here's a python doc on it that might be enlightening, if you really want to learn all the lies that surround FP you could start here. As simply as possible, with printf("%f\n", 0.1+0.1+0.1-0.3). I assume there's some gotcha I'm missing? edit: Oh, and the link to the Python docs pretty much answered my question. I guess I'll dig into the wikipedia article too, I find it pretty interesting. Double edit: Figured it out, printf's %f truncates it; %e shows the "correct" value. Thank you guys for being so helpful with my silly questions. One day I will be a good programmer and it will be awesome. Fergus Mac Roich fucked around with this message at 00:57 on Feb 24, 2015 |
# ? Feb 24, 2015 00:46 |
|
If you want to reproduce the exact same result make sure you're using double-precision floats.C code:
0.1 0.2 0.3 5.55112e-17
|
# ? Feb 24, 2015 01:01 |
|
Fergus Mac Roich posted:One day I will be a good programmer and it will be awesome. Don't worry, this mythical "good programmer" doesn't exist. You'll just find that you turn from a terrible programmer into a terrible programmer who knows they're terrible.!
|
# ? Feb 24, 2015 01:10 |
|
Thermopyle posted:Don't worry, this mythical "good programmer" doesn't exist. You'll just find that you turn from a terrible programmer into a terrible programmer who knows they're terrible.! This should probably be in the OP.
|
# ? Feb 24, 2015 01:16 |
|
fletcher posted:Got another question now...How come Pycharm is giving me warnings with this line? If you click on it then hover for a sec, it'll give you a little lightbulb that lets you properly install/import urlparse.
|
# ? Feb 24, 2015 01:19 |
|
EAT THE EGGS RICOLA posted:If you click on it then hover for a sec, it'll give you a little lightbulb that lets you properly install/import urlparse. No, in this case PyCharm just isn't smart enough to understand the way the six package manually adjusts the entries in sys.modules.
|
# ? Feb 24, 2015 01:48 |
|
Xandu posted:import.io was the first thing I tried, it's really quite cool, but I couldn't quite train it crawl the rest of the pages, though. I've been looking for an excuse to pick up some Python though, anyways. code:
1,2,3,4,5,6
|
# ? Feb 24, 2015 02:25 |
|
You are fantastic! That also strips out the obnoxious /n/t/t/t that was showing up.
|
# ? Feb 24, 2015 03:29 |
fletcher posted:Why is --no-site-packages not working? I can't figure out how six 1.8.0 is making it into my virtualenv, I didn't think I even had to specify --no-site-packages since it should be the default behavior, it doesn't seem to work either way though. Anybody know what was going on with this madness? The only way I could fix it was by uninstalling six from my global install.
|
|
# ? Feb 24, 2015 19:52 |
|
Question about structuring loops in python: I'm trying to make a loop that will restart at a different point depending on the outcome of checking a condition. I've tried googling, but can't seem to phrase the question right. I've diagrammed what I mean below. In this case, A is computationally expensive, so I'm trying to avoid it by checking the condition at C and just restarting the loop from B if things are "good enough." Any help is appreciated.
|
# ? Feb 25, 2015 04:37 |
|
You could put a short-circuit within A:Python code:
|
# ? Feb 25, 2015 05:15 |
|
Why not set a skip flag at C, and check it at A to see whether the stuff needs doing? Just make sure it's initially false so you process A the first time around (Which is the same basic idea as the above but with the logic in the loop itself, whichever makes most sense in your program's structure) baka kaba fucked around with this message at 05:30 on Feb 25, 2015 |
# ? Feb 25, 2015 05:27 |
|
SurgicalOntologist posted:You could put a short-circuit within A:
|
# ? Feb 25, 2015 11:00 |
|
I've been using the community version of PyCharm on my laptop for awhile now, and I love it but I don't think that I have a solid understanding of what advantages are offered by a Professional license. Is it basically only useful if you're doing webapp development? I've looked at the editions comparison page, but I don't understand what most of this stuff is (jinja2, pyramid, appengine? These are not things that I've ever used. I've at least heard of django and flask, in passing, but I don't use those either) The one thing that I am familiar with that's on the Pro list is SQL support. I have noticed that the community edition isn't checking SQL statements, so that basically comes with Pro? Are there any other significant advantages that I'm missing? Am I hamstringing myself and not realizing it? All of my work is just tinkering around, so I'm not worried about the lack of a commercial license, but I would like to support JetBrains if there's a tangible advantage to the professional license
|
# ? Feb 25, 2015 19:19 |
QuarkJets posted:I've been using the community version of PyCharm on my laptop for awhile now, and I love it but I don't think that I have a solid understanding of what advantages are offered by a Professional license. Is it basically only useful if you're doing webapp development? I've looked at the editions comparison page, but I don't understand what most of this stuff is (jinja2, pyramid, appengine? These are not things that I've ever used. I've at least heard of django and flask, in passing, but I don't use those either) Yup I think the real benefits of the pro version are when you are doing webapp development. That's when support for all those web frameworks and CSS preprocessors and such really shine. Another thing I really like with the pro version of PyCharm is being able to run tests with code coverage reports. For $99 it's a no-brainer for me. If you are just tinkering around and not doing webapp stuff with those frameworks, I'd say community edition is probably fine for you.
|
|
# ? Feb 25, 2015 19:57 |
|
QuarkJets posted:I've been using the community version of PyCharm on my laptop for awhile now, and I love it but I don't think that I have a solid understanding of what advantages are offered by a Professional license. Is it basically only useful if you're doing webapp development? I've looked at the editions comparison page, but I don't understand what most of this stuff is (jinja2, pyramid, appengine? These are not things that I've ever used. I've at least heard of django and flask, in passing, but I don't use those either) For me the killer pro feature is the remote interpreter / debugger. It lets you have a setup that feels like you're doing local development on your laptop but really everything is being executed on your big beefy cloud server.
|
# ? Feb 25, 2015 21:42 |
|
So I'm starting to build python apps that, while still pretty small in scope(single-threaded, totally local, etc), are big enough to warrant multiple classes and the like. I'm still a beginner and my learning background is in C, so I don't really have a good grasp of object oriented programming. I understand, on a basic level, the ideas behind encapsulation, inheritance, and polymorphism, but what I'm struggling with is how to put it all together and create a program with a really good, idiomatic structure. I'm looking for Python-specific tips, concepts, or hopefully reading material on how to master this aspect of programming. Here's an example of a challenge I'm facing with a small program. I'm writing an application that more or less acts as a leaderboard/record for performance in a board game(I have a group that meets once weekly). I've got some idea of how I want to structure my data in terms of classes. I have a player class that contains a name and methods that take a dictionary of games(keyed with a date object that's also a member of each game object). It also has __hash__ and _eq__ methods. I have a game class that contains said date object, and also a dictionary of player objects as keys with their respective scores as values. Game objects can also tell you who won them, and maybe some other things if I can think of some cool stuff to throw in there. I'm fine up to that point; there's almost definitely ways to improve upon this arrangement, but I think I can figure them out on my own. My issue is that, to manage it all, I've essentially created a class of which there is only ever one instance that manages dictionaries* of all the games we've played and all the players who have played by prompting the user for commands and then either returning or inserting the appropriate data. I'm concerned that in doing this, I've created a "God" class that's as bad as just a global state. Is this the violation of OOP that I perceive it to be? If so, how do I overcome it? I can't figure out what kind of structure I need to come up with that can avoid a class that sees and manages all the data. *I will probably change the top-level dictionaries to shelves for persistence. Fergus Mac Roich fucked around with this message at 02:05 on Feb 26, 2015 |
# ? Feb 26, 2015 02:02 |
|
It sounds like you should use a database ORM like SQLalchemy. I'm not sure what your God class looks like, but abstract wrapper classes are common in OOP. More common in Java than Python. I personally don't use them. Post example code. Don't try to make everything a class. Dominoes fucked around with this message at 02:19 on Feb 26, 2015 |
# ? Feb 26, 2015 02:10 |
|
Nippashish posted:For me the killer pro feature is the remote interpreter / debugger. It lets you have a setup that feels like you're doing local development on your laptop but really everything is being executed on your big beefy cloud server. The pro features I use the most are the Remote interpreter/debugger, the REST client, SQLAlchemy support, VM Support (full Vagrant integration), and Database stuff. And also the new Jinja2 stuff, but you're not using that at all.
|
# ? Feb 26, 2015 02:11 |
|
EAT THE EGGS RICOLA posted:The pro features I use the most are the Remote interpreter/debugger, the REST client, SQLAlchemy support, VM Support (full Vagrant integration), and Database stuff. I always forget about the REST client. I've never used it but come across it in the online help or wherever from time to time and think "I should try that out". I should try that out.
|
# ? Feb 26, 2015 03:59 |
|
If you're a college student or faculty member, you can get a free academic license for PyCharm Professional Edition and save $99 by just filling out this form - https://www.jetbrains.com/eforms/academicRequest.action?licenseRequest=PCP02NS
|
# ? Feb 26, 2015 10:18 |
How do I set a cookie in a requests session? edit: Ah! code:
fletcher fucked around with this message at 19:20 on Feb 26, 2015 |
|
# ? Feb 26, 2015 19:10 |
|
I'm trying to make me program easier for my users and am a bit stumped on something. We have an account number that ends in a Julian date. For instance: 22365003. I need to key off of the last four numbers, the "5003". The "5" is for 2015, and the "003" is the third day of the year. So the date associated with this account number is 01/03/2015. With an account number of 436114028, I'd like to know that the associated date is 01/28/2014. Any ideas on how to convert the last four numbers to a date format of MM/DD/YYYY?
|
# ? Feb 26, 2015 19:52 |
|
|
# ? May 9, 2024 20:40 |
|
Hughmoris posted:Any ideas on how to convert the last four numbers to a date format of MM/DD/YYYY? Python code:
|
# ? Feb 26, 2015 20:04 |