|
I've been hacking away at a little game project in Python, and I stumbled across this problem. I know the solution is probably something trivial but I can't track it down and I'm increasingly bugged. My question: I have a method that takes a class as an argument. How do I tell the method to make a new object of that class? I guess you could use __new__ but that seems odd.
|
# ? Oct 7, 2016 16:39 |
|
|
# ? May 9, 2024 05:47 |
|
LochNessMonster posted:I can imagine it's difficult to understand as you can't see the html source I'm trying to parse. I (now) understand the general idea you were showing me and I think I found a way to make that work, so thanks for that! Aww yea You should definitely read up on classes and stuff, just so you're aware of the general idea (even if you don't end up using them). It's good to be aware of this general idea of being able to drop in different objects and use them in some consistent way, so you can separate the specifics and reuse the general bit https://learnpythonthehardway.org/book/ex40.html that looks decent Hughmoris posted:My python skills (and programming in general) are pretty amateur so I'm walking through each solution and trying to understand how it works. Thanks for taking the time to write these up, and I'll let you know how it turns out. Oh I'd have commented it more if I'd known - if you want to know anything, just ask! Eela6's solution and mine are basically the same, we're just constructing the data a bit differently. Mine basically goes like this (it's like a pipeline) Python code:
The way I wrote mine, it just reads one record at a time, and passes it right through to the end (maybe storing it in the dictionary that remembers the last time a patient left the ICU). It's only the last line that finally builds a list - but you could just write those results to a file instead, one thing at a time. If your CSV data isn't sorted though, you need to collect it into a list so you can sort it all, so that's why the sorted(timestamped_transfers, key=lambda x:x[TIME]) line is there (that spits out a list, so you need to assign the result to a variable and make the next line run on that). It happens after you've filtered out the records you don't care about though, so it at least cuts things down You might not care about any of that, and if it's a small dataset then it probably won't matter anyway, but I'm into it! I think it's this guy's fault http://www.dabeaz.com/generators-uk/ The pdf of the slides is really readable on its own
|
# ? Oct 7, 2016 17:49 |
|
Vivian Darkbloom posted:I've been hacking away at a little game project in Python, and I stumbled across this problem. I know the solution is probably something trivial but I can't track it down and I'm increasingly bugged. From looking around, this is possible but it's pretty hacky, which makes it seem like you're not really meant to do this kind of thing. What exactly are you doing? If you lay out the general plan then people might have some more, uh, pythonic suggestions
|
# ? Oct 7, 2016 17:51 |
|
Vivian Darkbloom posted:I've been hacking away at a little game project in Python, and I stumbled across this problem. I know the solution is probably something trivial but I can't track it down and I'm increasingly bugged. Just call the class. Python code:
Space Kablooey fucked around with this message at 17:55 on Oct 7, 2016 |
# ? Oct 7, 2016 17:52 |
|
baka kaba posted:From looking around, this is possible but it's pretty hacky, which makes it seem like you're not really meant to do this kind of thing. What exactly are you doing? If you lay out the general plan then people might have some more, uh, pythonic suggestions It's not hacky, it's just taking advantage of first-class objects, and you'd treat it as any other callable.
|
# ? Oct 7, 2016 18:00 |
|
chutwig posted:It's not hacky, it's just taking advantage of first-class objects, and you'd treat it as any other callable. Oh is it that easy? All the SO answers had a billion variations on One Weird Reflection Trick. That's cool then
|
# ? Oct 7, 2016 19:14 |
|
Yeah it turns out that SO is actually pretty terrible most of the time e: With the section of this post, although technically it's the mathematica domain: http://mathematica.stackexchange.com/questions/66538/how-do-i-draw-a-pair-of-buttocks
|
# ? Oct 7, 2016 19:48 |
|
subway masturbator posted:Just call the class. Perfect, I knew it had to be something sensible like this. baka kaba posted:From looking around, this is possible but it's pretty hacky, which makes it seem like you're not really meant to do this kind of thing. What exactly are you doing? If you lay out the general plan then people might have some more, uh, pythonic suggestions Honestly I don't even have a special use case that demands this, I just got curious. What I'm doing (and I recognize I may not be doing it in the most efficient manner) is that I had dictionaries of the different units that players are allowed to deploy. The dictionary for a player has keys for each unit class and values for how many the player is allowed to field. Then when the player picks unit X to deploy, the placement method looks it up by class. Man, that was a complicated way to implement this.
|
# ? Oct 7, 2016 19:55 |
Subway masturbator is 100% correct. To further expand on his point, you can also handle positional and keyword arguments with *args and **kwargs! I made a small example for a strategy game below. EG: Python code:
code:
Eela6 fucked around with this message at 20:55 on Oct 7, 2016 |
|
# ? Oct 7, 2016 20:52 |
|
I'm trying to understand Enums in Python 3.5 and I can't get the __init__ method to work. I'm trying to base my stuff off the "Planets" example in the official docs. However, I get an AttributeError (shown below). Any suggestions?code:
|
# ? Oct 8, 2016 04:03 |
In enums, self.name is reserved -that's the name you chose, i.e, 'thumb'. You don't need to specify it because you get it 'for free' as part of an Enum. Check it out!Python code:
code:
|
|
# ? Oct 8, 2016 04:10 |
|
Eela6 posted:In enums, self.name is reserved -that's the name you chose, i.e, 'thumb'. You don't need to specify it because you get it 'for free' as part of an Enum. Check it out! Well I feel a little silly for making that mistake. Thank you so much!
|
# ? Oct 8, 2016 05:15 |
|
Would it be possible to write a Pycharm plugin to support unicode (ie greek letter) shortcuts like in Ipython/jupyter/julia ? ie type '\gamma tab' to get the letter. If this is doable, I might try.
|
# ? Oct 8, 2016 14:25 |
|
If you just need a small number of them it'd be easier to just use live templates.
|
# ? Oct 8, 2016 16:16 |
|
That works; didn't know about the feature. Defining a live template for each greek char under settings, then setting its applicability to Python (or all, etc) works; hit ctrl+j, 'pi', tab. This is p much the same as ipython, but with ctrl+j instead of \. Edit: even simpler syntax: 'Delta', tab.... or 'Del' tab. Solved. It looks like you can save this as XML files under your phycharm directory/config/templates. Ie Greek letters.xml: XML code:
Dominoes fucked around with this message at 16:51 on Oct 8, 2016 |
# ? Oct 8, 2016 16:24 |
As tempting as it is to use identifiers in Linear A , it's widely considered non-pythonic.
|
|
# ? Oct 8, 2016 18:30 |
|
That's an SA forums artifact.
|
# ? Oct 8, 2016 19:11 |
|
Dominoes posted:That works; didn't know about the feature. It's great. I've got a couple dozen of them set up besides the one PyCharm comes with and I use them extensively to generate a ton of code by just typing a couple characters and pressing Tab.
|
# ? Oct 8, 2016 19:13 |
|
Dominoes posted:Would it be possible to write a Pycharm plugin to support unicode (ie greek letter) shortcuts like in Ipython/jupyter/julia ? ie type '\gamma tab' to get the letter. If this is doable, I might try. If this isn't an explicit piece of functionality already it's definitely possible with snippets. I'm sure I've done this in a JetBrains editor. Edit: oops there were more posts
|
# ? Oct 8, 2016 21:53 |
|
I'm having some issues upgrading a package in python. I tried to upgrade the python cassandra-driver package from version 3.6 to 3.7 using pip, but when I try to use the newly installed package with my desired python version of choice, I get an error: code:
EDIT: So I noticed a warning message with my pip install about not being able to use a pip cache in my home directory while running install commands with sudo. I uninstalled the package, then re-installed with the '-H' flag for sudo and it seems to have worked this time. Not sure if something in pip cache was screwed up and was somehow telling pip to install a version with the wrong unicode width, but in any case it seems to be fixed now sd6 fucked around with this message at 16:47 on Oct 11, 2016 |
# ? Oct 11, 2016 15:59 |
|
I would like to display color, bold, etc. output to the terminal. Apparently Colorama is a good library for making sure color codes display right on any OS. That author recommends either Blessings or Termcolor as a way to actually generate the color codes and such, any recommendations on that front? I tried to get Blessings working but when I import it I get: "NameError: name 'unicode' is not defined" when it tries to subclass 'unicode' (I am guessing this is a Python 2 to 3 issue). e: 3 minutes later, this looks good -- https://pypi.python.org/pypi/blessed/1.8.5 Vivian Darkbloom fucked around with this message at 05:58 on Oct 12, 2016 |
# ? Oct 12, 2016 05:55 |
|
Another (likely dumb) question about something I don't quite understand. I figured I'd take the following route with parsing different variations of a specific webstore template. Each dealer has a specific variation of div/span classes in which the price, milage and year info is stored. Since there are only a few options per value I'd like to scrape, I figured I'd write a loop that checks which dealer uses which variation. I store which dealersite uses which variation in dictionary with the dealername as key, and the values in a tuple. Python code:
Python code:
fake edit: while I'm typing this I'm realizing my error. If I don't wrap the tuple in quotes, and remove the quotes from the if flags[x] == "value" it treats the tuple as a tuple instead of a string...
|
# ? Oct 15, 2016 14:16 |
|
Is the python plugin for Eclipse or NetBeans any good?
|
# ? Oct 15, 2016 20:26 |
|
psydude posted:Is the python plugin for Eclipse or NetBeans any good? I've heard that the Eclipse python plugin is good. But I don't personally use it, as a PyCharm / vim user
|
# ? Oct 15, 2016 20:36 |
|
LochNessMonster posted:Another (likely dumb) question about something I don't quite understand. Yep, if you put something in quotes it's a string literal(people also use this for code comments in Python). It's actually not that different from how we use quotes in regular language. To steal part of an example from The Structure and Interpretation of Computer Programs: Chicago is the largest city in Illinois. "Chicago" has seven letters.
|
# ? Oct 15, 2016 23:14 |
|
So now I've built my scraper/parser and put the data in a sqlite3 db, but now comes the presentation. What's the easiest way to make a website do queries on the db to show the data. Can I do that with python too, or should I look into JavaScript for that?
|
# ? Oct 16, 2016 10:17 |
|
LochNessMonster posted:So now I've built my scraper/parser and put the data in a sqlite3 db, but now comes the presentation. Ahh, well if I knew you wanted to make a website from the data I would have started you on Django from the start. Since you've already got it in sqlite with your own schema, I'd probably use Flask. No Javascript required.
|
# ? Oct 16, 2016 15:38 |
|
Thermopyle posted:Ahh, well if I knew you wanted to make a website from the data I would have started you on Django from the start. When I started I didn't have a real idea what to do, I'm just trying to learn and think qbout new things to do when I've finished something. I was reading about Django and Flask a bit earlier and I was leaning towards Django as it appears to be a large framework which might suit additional features I might think about later. One of the thinks I'd like to do is pricetracking and visualize that with a graphing framework or something. Flask seems to be in really early development (version 0.10). Does that matter?
|
# ? Oct 16, 2016 17:41 |
|
LochNessMonster posted:When I started I didn't have a real idea what to do, I'm just trying to learn and think qbout new things to do when I've finished something. Django and Flask (with the right plugins) will accomplish the same tasks. When Django is advertised as "batteries included", it really means it. It includes database migrations, user authentication, admin panels, ORM, caching frameworks, etc. If you'll need all of that or even most of that, Django is an awesome project to work with and has fantastic documentation. We even have a Django thread here and people like Thermopyle are great helps in there. If you just need an easy way to pull info from a sqlite database and throw it onto a webpage, look at Flask. It can do everything Django can, you'll just need to install each piece as an extension. Don't worry about the 0.11 version number. The project has been around for 5+ years and is very well maintained and mature. Pinterest and LinkedIn use Flask at scale, and I'm sure there are more examples. Tigren fucked around with this message at 18:53 on Oct 16, 2016 |
# ? Oct 16, 2016 18:50 |
|
Django sounds awesome, but it might be overkill for a newbie like me. Will I be able to add other functionality (like creating graphs) to a Flask web application later on?
|
# ? Oct 16, 2016 20:04 |
|
LochNessMonster posted:Django sounds awesome, but it might be overkill for a newbie like me. Yes. Flask is Just Python™ and you can use it to make just about any type of web app.
|
# ? Oct 16, 2016 20:25 |
|
LochNessMonster posted:Django sounds awesome, but it might be overkill for a newbie like me. The creating graphs functionality is entirely independent from your web framework. If you just want interactive visualizations, you can look into Bokeh, which is developed by BigRedDot. If you want to display those interactive visualizations along with other content, Bokeh will integrate with Flask very nicely.
|
# ? Oct 16, 2016 21:27 |
|
Ok great to hear. Flask it is! I noticed BigRedDot post some stuff about Bokeh in this thread, and that actually gave me the idea. It looks awesome and I can't wait to try it out, but first Flask.
|
# ? Oct 16, 2016 22:02 |
|
LochNessMonster posted:Django sounds awesome, but it might be overkill for a newbie like me. Flask is fine, but I want to beat the drum I beat every time this comes up (this isn't directed at you, I'm just throwing this out into the ether): Django is batteries-included, but that doesn't make it inferior to Flask. It's kind of like python itself...just because Python has seemingly millions of libraries in its standard library doesn't mean that makes it hard or inferior in some way for writing a for loop. You can use Django to write the web equivalent of Hello World in basically as minimal a fashion as Flask. A complete Django site: Python code:
The nice thing about starting out with Django is that when you want to expand your site's capabilities, those capabilities are "in the box". With Flask, you've got to find a third-party project (or write it yourself) that implements the functionality you want and cross your fingers that it's well maintained and has as many eyes on it watching it for security and quality as Django does. The nice thing about starting out with Flask is that it's easier to get started with because the documentation and tutorials don't cover all the stuff that doesn't come with Flask. Thermopyle fucked around with this message at 22:57 on Oct 16, 2016 |
# ? Oct 16, 2016 22:18 |
|
I haven't found many of Django's batteries to be useful, though. The comment system is mostly a joke.
|
# ? Oct 16, 2016 22:38 |
|
Well you could say the same thing about Python itself. There's a lot of poor API's in the standard library. There's also lots of great stuff. Same goes for Django. In both cases, even a good portion of the poor libraries can at least get the job done in some manner.
|
# ? Oct 16, 2016 22:42 |
|
Suspicious Dish posted:I haven't found many of Django's batteries to be useful, though. The comment system is mostly a joke. You'll have to expand on this. Django's batteries, if and when you need them, are definately useful. The admin interface, ORM and auth system themselves are worth the price of admission. Throw in migrations and django-admin and you really are getting a lot of features out of the box. Thermopyle posted:Flask is fine, but I want to beat the drum I beat every time this comes up (this isn't directed at you, I'm just throwing this out into the ether): A similar Flask app: Python code:
|
# ? Oct 16, 2016 23:14 |
|
For websites, I find Flask more difficult to work with than Django. It seems like a good idea at first, but most websites use several of the following: Database/migrations, admin, user accounts, email. Flask requires third-party plugins for these that don't necessarily play well together. Updates in one may break another, imports get messy, and the project is more difficult than if you'd started with Django. If you're making something other than a website that uses a webserver, Flask would be easier.
|
# ? Oct 17, 2016 00:05 |
|
LochNessMonster posted:I noticed BigRedDot post some stuff about Bokeh in this thread, and that actually gave me the idea. Let me know if I can answer any questions
|
# ? Oct 17, 2016 02:19 |
|
|
# ? May 9, 2024 05:47 |
|
Need help with boto3 again . Is there a way to bypass JSON deserialization of incoming CloudWatch data? Having them in dict is nice and all, but one of my use cases is literally to write JSON to a file (that is then sent somewhere), and having the data flow go code:
|
# ? Oct 17, 2016 14:35 |