Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I'm very curious about Python. I've never used it, but can't help but see everybody praising it, while everybody puts down PHP. I figured I might as well give it a whirl, but I was wondering if anybody had a good comparison of how something is written in PHP vs what the equivalent would look like in Python, specifically geared towards web development.

Adbot
ADBOT LOVES YOU

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

duck monster posted:

PHP is however a pretty focused little web language, but not really that good for much else, in comparison with other languages.

What makes it not as good as python for a command line application? I've only done very basic things on the command line with php, but Python looks like it would be a lot more useful since it has GTK support.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I've decide to re-write a PHP webapp in Python since the PHP code is crap and I've been wanting to learn Python for awhile anyways. I was going to give Flask & SQLAlchemy a shot, seems to offer a bit more flexibility over something like Django. Any advice before I dive in?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Perfect timing for the sale! My PyCharm trial was just about to run out.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
What do you guys typically use for a production stack? apache2? gunicorn? nginx out in front of those? How do you handle deployments to a production environment?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

deimos posted:

@jesseenoller (the OP iirc) asked exactly this on twitter today, general answer seems to be:

varnish -> nginx -> gunicorn/uwsgi -> pypy if possible -> pgbouncer -> postgres

All being automated by Chef/Puppet/Salt/Ansible/Shell Scripts/Trained Monkeys.


Or to just use a PaaS provider (Heroku).


edit: It just struck me how many giants of Python have actually posted in this thread.

I've been meaning to learn how to use chef. Been playing around with it for the past couple hours and it is really neat. One question though, and this may not be the right thread for it, but how do I get the code for my app onto the server using chef? The code is in a remote git repo...should I use git to download the code in my chef recipe? I'm sure there's a million different ways this could be answered, just looking for something basic and sensible for now.

edit: This sounds like the answer to my question: http://docs.opscode.com/resource_deploy.html

fletcher fucked around with this message at 06:37 on Aug 9, 2013

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Trying to set up a new machine but I'm running into an error when I try to run the south migration. It seems to work fine on my dev machine running Mint, but when my chef recipe gets to this step on Ubuntu Server 12.04 it hits an error. Any ideas?

code:
$ PYTHONPATH=/var/www/myapp; DJANGO_SETTINGS_MODULE=myapp.settings /path/to/venv/bin/python /var/www/myapp/manage.py migrate
Running migrations for myapp:
 - Migrating forwards to 0001_initial.
 > myapp:0001_initial
TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK
edit: Fixed it - it was a database permissions issue

fletcher fucked around with this message at 20:46 on Aug 9, 2013

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
If you install something like uWSGI in a virtualenv, should any configuration files also live inside the virtualenv?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I've got a form that allows somebody to upload a CSV file, and then I want to read it and display it back to them so they can enter some additional information about what is in each column. What sorts of things do I need to be concerned about for validating the file and displaying it back to them? I was looking at csvvalidator but it seems to be more geared to already knowing the structure of the CSV, which I will not know. Also looked at python-magic but the first random CSV I tried it on came back as assembler source, UTF-8 Unicode text, so that didn't seem like the way to go. So should I just not do any validation before loading it up with unicodecsv? I was just gonna make the assumption that it's UTF-8 encoded, reliably detecting the encoding seems like a big pain in the rear end.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Dren posted:

What things do you want to validate?

As far as UTF-8 checking goes, you can check if something is valid UTF-8 but that doesn't tell you if it's gobbledy-gook or not.

I dunno, I'm always paranoid about anything user supplied, and trying to prevent them from causing any harm. I suppose all I can do in this case is make sure I can read the whole file with UTF-8 encoding, and let them know if there was a problem.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

QuarkJets posted:

I was given a Macbook Pro at my work, and after about a month of trying to develop on it I gave up and installed an RHEL6 virtual machine. I've never looked back. OSX is just not a very good code development environment, especially if you're coming over from Linux. It's probably pretty good if you're coming over from Windows.

I wouldn't wanna do dev work on my host OS anyways. There are just sooo many benefits to using virtual machines for everything, even if my host OS was my preferred OS to do dev work in, I'd still use virtual machines.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
What's an easy way to strip exif data from an image?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Dren posted:

What kind of image? jpeg?

exiftool is a widely available perl program/library that can do it very easily.
code:
exiftool -all= image.jpg
via http://stackoverflow.com/questions/2654281/how-to-remove-exif-data-without-recompressing-the-jpeg

If you're doing photo processing with any of the adobe tools (lightroom, photoshop) they have options to strip metadata (exif) when you export files as jpeg.

I hadn't really thought about other image formats, I thought it was only jpg that supported exif data.

I came across exiftool in my searching but I was hoping for something that was pure python. I suppose I'll use it anyways though.

The purpose is just to strip exif data from user uploaded images, don't wanna be distributing GPS coordinates when they upload photos from their phone.

Thanks!

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Interesting...I'm using PIL to read the exif data, I didn't realize you could edit it as well. How do you do that?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Is there an easy way to crop and resize animated gifs in Python? I found images2gif but I'm wondering if there's something that's...a little better supported. Maybe I'm better off going with some external utility like ImageMagick?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

fletcher posted:

Is there an easy way to crop and resize animated gifs in Python? I found images2gif but I'm wondering if there's something that's...a little better supported. Maybe I'm better off going with some external utility like ImageMagick?

To answer my own question from earlier...I think I'm gonna go with gifsicle to do this. I couldn't get imagemagick to crop an animated gif without mangling it. Gifsicle seems to do it no problem.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

the posted:

Not sure, but I did a ghetto work-around by just editing the file that I created within python and then using that to read from.

Turn on display of file extensions! I find myself compulsively changing that setting as soon as I sit down a computer that isn't my own.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
My IDE suggested I rewrite my conditional

code:
is_active = mydict.get('key')
if is_active == False:
    # do stuff
as

code:
is_active = mydict.get('key')
if not is_active:
    # do stuff
but that doesn't have the same behavior when 'key' is not in mydict right? I should just ignore the suggestion?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Opinion Haver posted:

But that has different behavior if key is not in mydict.

fletcher, maybe you should use a set of active (or inactive) things and do if 'key' not in actives? It depends on what you're trying to do, what the 'default' semantics are, etc.

It's a boolean, doesn't always have to be in mydict though. Variable name corresponds to a field on a REST API.

If it's not specified (key does not exist in mydict), I don't want it to # do stuff. From the replies it sounds like the IDE is suggesting something that is not a logic neutral change.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Suspicious Dish posted:

If you want to compare against a boolean explicitly, always use foo is False, as lots of things can compare to False that aren't actually False.

Ahh that makes sense, thanks!

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Red Robin Hood posted:

I have nearly 0 coding experience. I've done a fair bit of HTML, some CSS, and basic batch file scripting. Dabbled with PowerShell.

Is Python a good first language?

Yes, definitely!

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Python code:
class Thingy():
  cached_expensive_stuff = None

  def get_expensive_stuff(self):
    if self.cached_expensive_stuff is None:
      self.cached_expensive_stuff = []
      # expensive operation to fill the array with goodies
    return self.cached_expensive_stuff
I want to be able to call get_expensive_stuff() all willy nilly...is this an alright way to go about that? Or is there a better way?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

FoiledAgain posted:

You could put this in its own module and then do from thingy import cached_expensive_stuff.

My example might be a little too simplified. There's other attributes in Thingy() that are used in get_expensive_stuff, so I thought it made sense to make it a method on Thingy()

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Mr. Wynand posted:

Do you mean automating caching? That's more of a ruby thing to do (:smugdog:), but there is actually a memoization decorator in Python 3's functools module (lru_cache).

What you have looks fine. Like I'd be more likely to look at it closer during code review if you had used a fancy decorator than if you just did it explicitly like you did.

Yup, I had a feeling Python might have something fancy like that lru_cache. I'm still on 2.7 so I won't go down that route just yet. Thanks for the feedback!

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Mr. Wynand posted:

PS: make sure it's "class Thing(object):" - when you don't inherit from "object" you are defining "old style classes" which is a deprecation measure you should steer clear of.

Ah good to know. It's actually a Django model so it inherits from models.Model. I haven't actually declared any of my own classes yet outside of those models, but I will keep that in mind.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Haystack posted:

Lifted more or less unmolested from Pyramid's source

Well that's pretty drat slick, I don't quite understand everything going on in there though. What's it doing with __doc__?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
That was a great post to read, well done :)

I think I'll stick with the if statement for now.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

fletcher posted:

Python code:
class Thingy():
  cached_expensive_stuff = None

  def get_expensive_stuff(self):
    if self.cached_expensive_stuff is None:
      self.cached_expensive_stuff = []
      # expensive operation to fill the array with goodies
    return self.cached_expensive_stuff
I want to be able to call get_expensive_stuff() all willy nilly...is this an alright way to go about that? Or is there a better way?

After reading about this a bit more, I'm a little confused.

I was thinking cached_expensive_stuff = None was initializing an instance variable, not a class variable. I don't want different instances of Thingy using the same cached_expensive_stuff. But from my test, it doesn't seem like they are using the same cached_expensive_stuff, so it works like I intended?

Python code:
import uuid


class Thingy(object):
    cached_expensive_stuff = None

    def get_expensive_stuff(self):
        if self.cached_expensive_stuff is None:
            print('Doing expensive operation')
            self.cached_expensive_stuff = uuid.uuid4()
        return self.cached_expensive_stuff


print(Thingy.cached_expensive_stuff)

print('firstThing')

firstThing = Thingy()
print(firstThing.get_expensive_stuff())
print(firstThing.cached_expensive_stuff)

print('secondThing')

secondThing = Thingy()
print(secondThing.get_expensive_stuff())
print(secondThing.cached_expensive_stuff)

print('firstThing again')

print(firstThing.get_expensive_stuff())
print(firstThing.cached_expensive_stuff)

print(Thingy.cached_expensive_stuff)
Results:
code:
None
firstThing
Doing expensive operation
b62b35dd-0ecf-4aa5-9e3f-745ab27ed0d7
b62b35dd-0ecf-4aa5-9e3f-745ab27ed0d7
secondThing
Doing expensive operation
68b1ab53-0bb7-4e56-aa56-0ca18e720762
68b1ab53-0bb7-4e56-aa56-0ca18e720762
firstThing again
b62b35dd-0ecf-4aa5-9e3f-745ab27ed0d7
b62b35dd-0ecf-4aa5-9e3f-745ab27ed0d7
None
Is it both an instance and a class variable or something? I'm coming from Java land if that matters.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Pollyanna posted:

Tried to take the Python test on E-Lance and I couldn't even get past the first question :suicide:

The question was "write a problem that takes an input "n" and returns the maximum" blah blah gently caress I don't totally remember it was something about finding the maximum number of range(1, 9xn) that is evenly divisible by n. Like, 9x2 would be "99", and the result would be "98" since that's the maximum number that's evenly divisible by 2.

I submitted an answer for that one and ragequit when it was wrong. Especially since it was taking too long for me to write it. I may have freaked out a little too harshly :( Has anyone seen that problem before? Am I retarded for not finishing with 5 minutes or am I retarded for blowing the whole thing on one fuckup? (it's the latter)

Don't get so down on yourself and keep practicing. It takes time to build up the mental toolbox of things you can use to solve problems. You aren't alone in experiencing these frustrations. You do yourself a disservice though if you don't learn from these moments!

Sometimes for those types of problems, it's better to just bust out a pen and paper and play around with the numbers to figure out how your program might work.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
http://flask.pocoo.org/docs/templating/#controlling-autoescaping

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Woohoo! Way to go!!

Pollyanna posted:

  • If the URL is pointed to a stock symbol that does not exist, e.g. http://rpazyaquian.herokuapp.com/stocks/argleblargle , the page returns a 500 error. This needs to be changed so that a proper error page shows up, or the routing should properly handle an invalid symbol.
  • I want to have the URL for accessing a certain stock symbol to instead be something like '/stocks?symbol=GOOG' as opposed to '/stocks/GOOG/. I don't know how, though. Flask supports the GET method, but I'm not sure how it would work in this situation:
  • One drawback of how I approach the embed.js snippets is that every time a page is rendered, it creates a new embed.js file for reference. I can't help but think that this will become a problem in the future. What should I do to avoid this? Is there a way to add pure JS to the webpage to render the plot, or does that not make sense?

#1 Maybe use a try/except and catch the IOError (should be caught anyways when communicating to external services in case they go down)
#2 I think you want request.args, haven't used Flask before though
#3 Probably a way to avoid generating an actual file each time, not familiar with that library though. Should be able to just stick it inside of a <script type="text/javascript">alert('hello world')</script> type of thing. If it turns out you have to actually generate a file, use a randomized filename so you don't run into an issue where two requests try to write to the same embed.js file each time (and make sure they are temp files so they get automatically cleaned up)

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Pollyanna posted:

I don't know what happened since I started working on v1.2, but now SA keeps flashing "hello world" alert boxes at me :(

Hmmm that's not good...XSS vulnerability on here? I'm not getting any alerts on this page.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Dominoes posted:

I love how easily cross-platform Python and its tools are. I've been in a transient period for the past three months, using a laptop with Ubuntu. Since then, I picked up some new habits like using Git, Pycharm,IPython etc. To transition back to the Windows desktop, all I had to do was copy and past my code folder, and install git/pycharm/ipython. My programs and Django apps run with no additional configuration, PyCharm looks the same after setting up the fonts, and it recognizes my Qt etc modules now.

My only gripe is that Qt fonts don't automatically scale.

But why would you wanna develop on Windows?? Install an Ubuntu VM on your desktop :)

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Little test program:
code:
#!/usr/bin/env python


class CoolObject(object):
    def __init__(self, a=None, b=None):
        self.a = a
        self.b = b

    def __eq__(self, other):
        print('__eq__ called')
        if isinstance(other, self.__class__) and self.a == other.a and self.b == other.b:
            return True
        return False

the_list = []

original = CoolObject(a=1, b=2)
the_list.append(original)

duplicate = CoolObject(a=1, b=2)

print('original in the_list? %s' % (original in the_list))

print('duplicate in the_list? %s' % (duplicate in the_list))

print('duplicate NOT in the_list? %s' % (duplicate not in the_list))

print('original == duplicate? %s' % (original == duplicate))
Results:
code:
original in the_list? True
__eq__ called
duplicate in the_list? True
__eq__ called
duplicate NOT in the_list? False
__eq__ called
original == duplicate? True
Is __eq__ only called 3 times because it's just checking the reference in my first print()?

I have a function that generates a bunch of CoolObjects and returns a list, but as I'm generating them I don't want to add them to the list if a duplicate is already in there. Is overriding __eq__ the correct way to handle 'x in somelist' and 'x not in somelist' behavior?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
In case it's at position 0:

code:
>>> x = 'eggs over easy'
>>> if x.find('eggs'): print('found it!')
...
>>> 

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
That one is probably more suited to the Web Development megathread, may want to ask over there and provide some sample code.

I have a question about the Image.save() method in PIL/Pillow. I see examples of code using arguments like quality and optimize, but I can't find any details about what they do or how they work in the documentation. Where can I find out more details about them?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Is there any way to set the max number of http retries that a pip install will attempt? I keep getting this error trying to grab stuff off s3 from ec2:

quote:

Max retries exceeded with url: /pypi/uwsgi-1.9.18.2.tar.gz (Caused by <class 'httplib.BadStatusLine'>:

It seems odd that I can't reproduce it with this though:
code:
$ for i in {1..20}; do wget --delete-after --tries=1 -i requirements.txt; done

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Is there an easy way to see which packages are depending on package <x> in a virtualenv?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

accipter posted:

You could try this:
code:
pip install --no-install <PACKAGE NAME HERE>

That seems to work but only going the other direction:

code:
$ pip install --no-install Fabric==1.8.1
...
Requirement already satisfied (use --upgrade to upgrade): paramiko>=1.10.0 in /home/fletcher/.virtualenvs/tmp_env/lib/python2.7/site-packages (from Fabric==1.8.1)
...
But let's say I didn't know why paramiko got installed and I wanted to find out. Fabric doesn't get mentioned in the output (which makes sense):

code:
$ pip install --no-install paramiko==1.10.0 | grep -i fabric

Adbot
ADBOT LOVES YOU

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

SurgicalOntologist posted:

I'm annoyed by Pycharm's inspection of example code in docstrings (detected by >>>). For example, I have this docstring segment:
Python code:
    """
    Examples
    --------
    Assuming an `Experiment` named ``exp`` with levels ``['participant', 'session', 'block', 'trial']``:

    >>>some_block = exp.section(participant=2, session=1, block=3)
    """
Pycharm highlights exp and gives a "no reference found" warning. Now I understand that the proper convention would be to actually instantiate exp instead of describing it in a sentence, but in this case that would require 3-4 lines of code and it would be unwieldy to put that in every docstring example.

So- am I doing it wrong? If so, any suggestions?

If not, is there a way to turn this inspection off? I can't find it. It wasn't a big deal until I made a change and wanted to check that it didn't produce any extra warnings. Since my file is littered with these warnings I can't tell quickly.

Is that using the latest version of PyCharm? I do see several bug reports that sound like what you are describing. This one was closed as a wontfix - is your docstring the first statement in the function?

  • Locked thread