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
tripwire
Nov 19, 2004

        ghost flow

spankweasel posted:

For debugging strings, you can use %r to print the raw string:

code:
>>> a = "a string\n"
>>> print a
a string

>>> print "%r" % a
'a string\n'
>>> 
Not directed at you, but for python beginners:

There is a little more going on here than first meets the eye.

Objects have both a "str" and a "repr" method. The first gives a string representation of the object, usually focused being concise and human readable - like toString, if you come from java.

In contrast, repr generally gives a string representation of the object suitable for recreating that object totally; it shouldn't leave anything out. The object class defines a repr method which prints out the class name and the location in memory (using the id builtin function). The results of repr is what you see in interactive mode when you give the interpreter an expression consisting of just an object:
code:
>>> class Foo(object):pass
...
>>> Foo()
<__main__.Foo object at 0x7ff46b4c>
>>> class Bar(object):
...     def __repr__(self):
...         return "bananas"
...
>>> Bar()
bananas
>>>
String overrides the repr method of object to give you the raw string, keeping with the intention of repr.

And finally, the %r format specifier basically tells python to use repr when interpolating/formatting the object into the string, rather than str (which is what %s means).


Also secret python protip: backticks around an expression means call repr() on it. No one ever uses them.

tripwire fucked around with this message at 00:57 on Oct 7, 2011

Adbot
ADBOT LOVES YOU

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

tripwire posted:

Also secret python protip: backticks around an expression means call repr() on it. No one ever uses them.
This was removed in Python 3

raymond
Mar 20, 2004
Does anyone have any advice for finding good projects to work on? I've been trying to think of a good idea for literally years but can never think of anything. I want to find some cool project that people would actually use directly (probably a website), rather than contributing to a platform project like Django or whatever.

I think the answer is "stop being so picky" but hey, I might as well ask.

Edit: I found this, but it's totally dead.

http://idk.codeplex.com/ posted:

Every other month, IDK asks people to come up with their best ideas for new programs and websites. At the end of the month, everyone goes to work, and we bring our best ideas to life. Have a great idea? Let IDK build it for you.

raymond fucked around with this message at 07:40 on Oct 8, 2011

TasteMyHouse
Dec 21, 2006
start working on an existing project, then you'll find a million things you wish existed that don't.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

TasteMyHouse posted:

start working on an existing project, then you'll find a million things you wish existed that don't.

This is so true.

FoiledAgain
May 6, 2007

raymond posted:

Does anyone have any advice for finding good projects to work on? I've been trying to think of a good idea for literally years but can never think of anything.

This came up earlier in the thread I think, but a good place to start is your everyday work. What's something you have to do a lot that you wish was automated? Make it so. Use the 10 minute rule: if something takes more than 10 minutes and you have to do it more than once, you should write some code that takes care of it for you.

vvvvvv Hahaha....sorry, this is pretty elementary advice!

FoiledAgain fucked around with this message at 03:54 on Oct 9, 2011

raymond
Mar 20, 2004

TasteMyHouse posted:

start working on an existing project, then you'll find a million things you wish existed that don't.
Absolutely, but my problem is finding a project!

FoiledAgain posted:

This came up earlier in the thread I think, but a good place to start is your everyday work. What's something you have to do a lot that you wish was automated? Make it so. Use the 10 minute rule: if something takes more than 10 minutes and you have to do it more than once, you should write some code that takes care of it for you.
I probably should have said that I'm a full-time Python developer looking for a project outside of work. Basically, I want a side-project but I'm really unimaginative as far as project ideas go.

I might look into freelance work, I guess.

spankweasel
Jan 4, 2006

raymond posted:

Absolutely, but my problem is finding a project!

I probably should have said that I'm a full-time Python developer looking for a project outside of work. Basically, I want a side-project but I'm really unimaginative as far as project ideas go.

I might look into freelance work, I guess.

Since you're a full-time Python dev, why not look into making existing tools better:

pylint - add a feature to fix some/most/all of whatever inane bullshit it thinks is wrong with your code (hurr this method has too many conditional statements!)
pep8 - add a feature to fix discovered errors (this has been done already by a fork of the project, but write up a patch for the original guy)
nose - add features to nose that you need for testing.
mercurial - write a useful hook or help fix what mercurial currently uses (I have no idea how popular hg actually is, but it's what my shop uses. I guess all the cool kids use git now or something.)
python - find and fix bugs in python itself. They have a huge need for unittests among many many MANY other things.

duck monster
Dec 15, 2004

raymond posted:

Does anyone have any advice for finding good projects to work on? I've been trying to think of a good idea for literally years but can never think of anything. I want to find some cool project that people would actually use directly (probably a website), rather than contributing to a platform project like Django or whatever.

I think the answer is "stop being so picky" but hey, I might as well ask.

Edit: I found this, but it's totally dead.

Fork boa constructor, and bring it back from the dead. Seriously. If I had time I would do it. Its the best python RAD ide ever, but it suffers terrible dependency rot at the moment. Its basically delphi but with python. But it was a one man job and I think he gave up a bunch of years ago.

duck monster fucked around with this message at 10:14 on Oct 10, 2011

raymond
Mar 20, 2004
While I do very much appreciate the suggestions, I can't get excited about writing dev tools in my spare time. I do enough laborious development at work, so a home project needs to be fun. I'll just keep watching TV shows and playing video games while hopelessly trying to think up the next Twitter or Reddit (minus the CP).

Zombywuf
Mar 29, 2008

raymond posted:

While I do very much appreciate the suggestions, I can't get excited about writing dev tools in my spare time. I do enough laborious development at work, so a home project needs to be fun. I'll just keep watching TV shows and playing video games while hopelessly trying to think up the next Twitter or Reddit (minus the CP).

You really have 2 options.

1) Flashy graphical things which can offer quick feedback for your effort and fun optimisation problems. Try writing a tunnel raytracer in Python, aim for 60fps :-)

2) Tools. Try and find something you need and implement it. Probably the tool I'm most proud of making is Xawk https://github.com/SteveJones/xawk. It's really lovely but it's surprisingly useful. Maybe one day I'll finish it. curl 'http://forums.somethingawful.com/' | ./hawk -e '//a[@class="forum"] { print text() " - " @title; }' Gets you a list of the top level sub forums.

Zombywuf fucked around with this message at 10:35 on Oct 11, 2011

maskenfreiheit
Dec 30, 2004
Edit: doublepost

maskenfreiheit fucked around with this message at 01:23 on Mar 13, 2017

posting smiling
Jun 22, 2008

GregNorc posted:

Hi. I'm a bit of a python newbie. I use it mostly to clean files before I do stat work in them in R.

I was wondering: could having a period in the argument of a python program cause an error?

Example:

I run "python oneDigit.py raw.csv", which loads "raw.csv" and basically outputs the contents after running a regex to strip out useless whitespace.

Instead I get this error:


The code is on http://pastebin.com/08EqF7Zn in case there's just something wrong with my code... but I think the issue is the period in "raw.csv"

I know the error is something to do with having the filename be a command line flag - when I hard coded the filename, it ran perfectly. Any help is greatly appreciated.

argv[0] is the name of your script, not the first argument. http://docs.python.org/library/sys.html#sys.argv

maskenfreiheit
Dec 30, 2004
Edit: doublepost

maskenfreiheit fucked around with this message at 01:23 on Mar 13, 2017

MC Fruit Stripe
Nov 26, 2002

around and around we go

TasteMyHouse posted:

You're making things harder on yourself by using Python 3 (and windows...)
Are the two so incompatible? Would one be better off learning Powershell, or any other alternative, rather than Python, if they intend to use it in a Windows environment?

TasteMyHouse
Dec 21, 2006

MC Fruit Stripe posted:

Are the two so incompatible? Would one be better off learning Powershell, or any other alternative, rather than Python, if they intend to use it in a Windows environment?

I personally love python, and it isn't actually that bad on Windows. I was just being an OS snob :)


Learn powershell if your goal is to Command Windows. learn python if you want to write software.

Lurchington
Jan 2, 2003

Forums Dragoon

GregNorc posted:

I run "python oneDigit.py raw.csv", which loads "raw.csv" and basically outputs the contents after running a regex to strip out useless whitespace.


since you're actually doing work with those csv files instead of just doing some tutorials, python's csv module is really useful and may be worth considering depending on what exactly you're doing with regular expressions.

And if you ever get data in excel format, there's also xlrd

maskenfreiheit
Dec 30, 2004
Edit: doublepost

maskenfreiheit fucked around with this message at 01:23 on Mar 13, 2017

rivals
Apr 5, 2004

REBIRTH OF HARDCORE PRIDE!
I apologize if this gets asked a lot but I skimmed a bit a didn't see anything. I'm looking for a simple and preferably cheap way to host Python webapps. I've never done web dev in Python, just PHP, and I'm looking to start playing around with Python on the web and trying flask. I used to have hosting through a friend (using a LAMP stack) but he shut down his servers. I tried using AWS and doing all of the admin stuff myself but after fighting with fastcgi config on lighttpd for a while I give up. I have a domain, dns hosting, etc already I just want something I can use to deploy python webapps with as little headache as possible. Any suggestions are appreciated. I've been recommended heroku, dotcloud, and google app engine but all by people who have heard good things about them, not from people who have used those services, so some hands on opinions would be great.

Lurchington
Jan 2, 2003

Forums Dragoon
I have a barebones VPS over at Rackspace Hosting (formerly slicehost), and I although I don't actually do much with it I found it very easy to get up to speed. I'm more comfortable installing things for myself with yum/apt on the command line, and if that sounds ok, it's hard to get any easier

duck monster
Dec 15, 2004

Has anyone know where I can get a prebuilt version of psycopg2 and the various geodjango binary bits for Lion. This loving upgrade (which I had to do to use the latest xcode for work) has blown away my python site-packages directory (!!!) and for the loving life of me I cant work out how to do this since I have no idea where to find the Python.h (etc) relevant to my Lion python2.7 install, or for that matter how to tell easy_install where it is.

e: Hmm seems like that xcode install didn't take. Trying again...

duck monster fucked around with this message at 11:24 on Oct 17, 2011

Zombywuf
Mar 29, 2008

duck monster posted:

Has anyone know where I can get a prebuilt version of psycopg2 and the various geodjango binary bits for Lion. This loving upgrade (which I had to do to use the latest xcode for work) has blown away my python site-packages directory (!!!) and for the loving life of me I cant work out how to do this since I have no idea where to find the Python.h (etc) relevant to my Lion python2.7 install, or for that matter how to tell easy_install where it is.

I had a lot of luck with macports. Just remember that you want 2.7 then tell it "py27-" every time. Once I'd used it to install setuptools then easy_install worked fine.

duck monster
Dec 15, 2004

Ugh. This is a loving mess. I'm going to try and restore my old 2.6 dir from time machine.

*shudder*

e: haha gently caress you lion I win. It worked.

Time machine owns.

duck monster fucked around with this message at 12:31 on Oct 17, 2011

Johnny Cache Hit
Oct 17, 2011

rivals posted:

I apologize if this gets asked a lot but I skimmed a bit a didn't see anything. I'm looking for a simple and preferably cheap way to host Python webapps. I've never done web dev in Python, just PHP, and I'm looking to start playing around with Python on the web and trying flask. I used to have hosting through a friend (using a LAMP stack) but he shut down his servers. I tried using AWS and doing all of the admin stuff myself but after fighting with fastcgi config on lighttpd for a while I give up. I have a domain, dns hosting, etc already I just want something I can use to deploy python webapps with as little headache as possible. Any suggestions are appreciated. I've been recommended heroku, dotcloud, and google app engine but all by people who have heard good things about them, not from people who have used those services, so some hands on opinions would be great.

Amazon AWS is excellent. Plus you will probably be able to fit in their free usage tier (http://aws.amazon.com/free/).

I've used Google App Engine, and it's nice, but if you're just getting started with Python development I wouldn't recommend it -- stay vanilla, use AWS.

duck monster
Dec 15, 2004

Webfaction is prety good for cheap hosting,

Chimp_On_Stilts
Aug 31, 2004
Holy Hell.
I want to learn to use Python to help with website analytics. Data collection and analysis from Google Analytics, KISSmetrics, Mixpanel, Optimizely, etc. I'm confident my needs are fairly straightforward. What is the best way/source to learn Python with this need in mind?

I've browsed some options, and right now I'm attracted to Learn Python the Hard Way, but I worry that it focuses mainly on building small applications rather than querying something like Google Analytics for the kind of data I need. Then again, maybe everybody including me needs those basic skills, or maybe writing those kinds of mini-apps is exactly what I need to be doing and I just don't understand that.

My experience with coding is rudimentary, please forgive any misapprehensions re. what I need to learn. I'd like to learn as quickly as possible, but I'm not under the delusion that I'll get this down in just a couple days with near-zero prior experience.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Chimp_On_Stilts posted:

I want to learn to use Python to help with website analytics. Data collection and analysis from Google Analytics, KISSmetrics, Mixpanel, Optimizely, etc. I'm confident my needs are fairly straightforward. What is the best way/source to learn Python with this need in mind?

I've browsed some options, and right now I'm attracted to Learn Python the Hard Way, but I worry that it focuses mainly on building small applications rather than querying something like Google Analytics for the kind of data I need. Then again, maybe everybody including me needs those basic skills, or maybe writing those kinds of mini-apps is exactly what I need to be doing and I just don't understand that.

My experience with coding is rudimentary, please forgive any misapprehensions re. what I need to learn. I'd like to learn as quickly as possible, but I'm not under the delusion that I'll get this down in just a couple days with near-zero prior experience.

There is nothing that would teach you anything geared towards what you want...that doesn't even really make any sense. You just need to learn Python. Period.

From what I understand Python the Hard Way is a fine place to start.

maskenfreiheit
Dec 30, 2004
Edit: doublepost

maskenfreiheit fucked around with this message at 01:23 on Mar 13, 2017

Opinion Haver
Apr 9, 2007

You can do it simpler than that:

code:
def allsums(l):
    if len(l) == 0: return [0]
    rest_sums = allsums(l[1:])
    return [x + y for x in [0, l[0]] for y in rest_sums]
Basically, to find the list of all the sums, you either include or exclude the first element in the sums of the rest. If you only want unique values, you can do set(allsums(l)). Note that this will include the possibility of summing nothing, giving you zero.

No Safe Word
Feb 26, 2005

GregNorc posted:

Let's say I have a list of five integers. For sake of simplicity, l = [1,2,3,4,5]

I want to calculate all the possible ways they could be combined.

For example:
1 + 2
1 + 3,
1+ 4,
1+ 5,
1 + 2 + 3
1 + 2 + 4
(and so on)

I found a library called itertools that lets me generate lists, but none of it's outputs meet my needs (http://docs.python.org/library/itertools.html#itertools.combinations)

I'm really having trouble finding help since there is probably some specific phrasing used to describe the type of combination I am attempting and since I am not articulating properly, I can't find the answer.

I am kind of freaking out because this is part of a coding assignment that is due tomorrow. I thought it was due next week since all my other professors pushed deadlines until after a major academic conference. Which is my own fault, but it is annoying since I do not have the luxury of writing an elegant solution.

It's basically the knapsack problem: given a list S give all the values that added together are less than some maximum.

Due to my time constraints I'm attempting a simple brute force solution: generate all the possible combinations. Sum all the possible combinations. Spit out all the combinations whose total are less than a certain threshold.

If anyone can help, I'd greatly appreciate it.
If you want to retain the combinations as well as the sums you can do something like:

code:
>>> L = [1,2,3,4,5]
>>> combos = []
>>> for x in range(1,len(L)+1):
...     combos += itertools.combinations(L, x)
...
>>> [(tup, reduce(operator.add, tup)) for tup in combos]
[((1,), 1), ((2,), 2), ((3,), 3), ((4,), 4), ((5,), 5), ((1, 2), 3), ((1, 3), 4), ((1, 4), 5), ((1, 5), 6),
 ((2, 3), 5), ((2, 4), 6), ((2, 5), 7), ((3, 4), 7), ((3, 5), 8), ((4, 5), 9), ((1, 2, 3), 6), ((1, 2, 4), 7),
 ((1, 2, 5), 8), ((1, 3, 4), 8), ((1, 3, 5), 9), ((1, 4, 5), 10), ((2, 3, 4), 9), ((2, 3, 5), 10), ((2, 4, 5), 11),
 ((3, 4, 5), 12), ((1, 2, 3, 4), 10), ((1, 2, 3, 5), 11), ((1, 2, 4, 5), 12), ((1, 3, 4, 5), 13),
 ((2, 3, 4, 5), 14), ((1, 2, 3, 4, 5), 15)]
>>> d = dict(_)
>>> d[(1,)]
1
>>> d[(1,2,3)]
6
(I think python 2.7 has the dictionary comprehensions that allow you to skip the dict() step, but I did this in 2.6)

edit: fixed tables

maskenfreiheit
Dec 30, 2004
Edit: doublepost

maskenfreiheit fucked around with this message at 01:24 on Mar 13, 2017

No Safe Word
Feb 26, 2005

GregNorc posted:

Thanks...
The problem is I need to remember what numbers were summed.

so if the output was 6, I need to see if 2+3+1 or 4+2 or (insert another possibility here) was what lead to it.

You can use my solution above and just sort the tuple you're checking and use that as a key. Or you can key it on the sum instead, though multiple combinations could have the same sum, which would make it a little different ...

good jovi
Dec 11, 2000

EDIT: dang, so slow.

if you actually care about seeing all the different combinations, something like this should get you there:

code:
import itertools
a = [1,2,3,4,5]
b = list(
    itertools.chain.from_iterable(
        itertools.combinations(a, i) for i in range(1, len(a) + 1)
    )
)
print b
Since combinations requires a length parameter, you just need to generate the list of combinations for each length, then mash 'em together.

Johnny Cache Hit
Oct 17, 2011

Sailor_Spoon posted:

EDIT: dang, so slow.

if you actually care about seeing all the different combinations, something like this should get you there:

code:
import itertools
a = [1,2,3,4,5]
b = list(
    itertools.chain.from_iterable(
        itertools.combinations(a, i) for i in range(1, len(a) + 1)
    )
)
print b
Since combinations requires a length parameter, you just need to generate the list of combinations for each length, then mash 'em together.

You don't need to check |a|+1, as C(n,k)=0 for k>n.

Otherwise, this code should be exactly what GregNorc wants. There's no specific phrasing to it -- it's just a plain ol' combination without repetition.

No Safe Word
Feb 26, 2005

Kim Jong III posted:

You don't need to check |a|+1, as C(n,k)=0 for k>n.

Otherwise, this code should be exactly what GregNorc wants. There's no specific phrasing to it -- it's just a plain ol' combination without repetition.

the +1 is to pull in the original since range is not endpoint-inclusive

Hanpan
Dec 5, 2004

I am trying to figure out how to structure my Python application. It is basically a whole bunch of producers and workers which I need to keep up and running indefinitely.

The way I saw this working was to create a 'supervisor' script that I would run as a daemon or using upstart. This supervisor will somehow spawn the all the producers and workers, potentially as sub processes? I need to create it in such a way so if I was to stop the supervisor process, it will shut down all the spawned processes first before exiting.

I'd also like to be able to implement some kind of event/messaging system using a broker. What I'd like to be able to do is listen for system wide events on the supervisor and then communicate the event to the relevant sub process.

Can anyone tell me how I can go about achieving this? Is using a singular supervisor script with spawned sub processes correct or am I missing something?

Johnny Cache Hit
Oct 17, 2011

No Safe Word posted:

the +1 is to pull in the original since range is not endpoint-inclusive

:ughh:

Man, the fact that range is closed on the left and open on the right always screws me up... and I've been using Python for years now.

TasteMyHouse
Dec 21, 2006

Kim Jong III posted:

:ughh:

Man, the fact that range is closed on the left and open on the right always screws me up... and I've been using Python for years now.

this has never bothered me, because it's the same way the standard for loop works in C-derived languages (i.e. using <, and not <=)

Haystack
Jan 23, 2005





Hanpan posted:

I am trying to figure out how to structure my Python application. It is basically a whole bunch of producers and workers which I need to keep up and running indefinitely.

The way I saw this working was to create a 'supervisor' script that I would run as a daemon or using upstart. This supervisor will somehow spawn the all the producers and workers, potentially as sub processes? I need to create it in such a way so if I was to stop the supervisor process, it will shut down all the spawned processes first before exiting.

I'd also like to be able to implement some kind of event/messaging system using a broker. What I'd like to be able to do is listen for system wide events on the supervisor and then communicate the event to the relevant sub process.

Can anyone tell me how I can go about achieving this? Is using a singular supervisor script with spawned sub processes correct or am I missing something?

Check out Supervisor for a pre-rolled solution.

Adbot
ADBOT LOVES YOU

Jaur
Jun 5, 2005

uguu~
I decided to learn some Python with Pygame (I have little to no real programming experience), and I downloaded this off of the pygame web page.

I got the the fourth "chapter", where pygame gets introduced, and every time I use pygame to open up a window, it crashes when I close it (hangs/becomes unresponsive/I have to task manager it). This is only through IDLE though, when I run the file straight through python, it works fine (basically).

This is my code (basically lifted straight out of the pdf):

code:
import pygame
pygame.init()

black = [  0,  0,  0]
white = [255,255,255]
blue =  [  0,  0,255]
green = [  0,255,  0]
red =   [255,  0,  0]

pi = 3.141592653

size=[400,400]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("augh yeah")

done=False

clock = pygame.time.Clock()

while done == False:
    pygame.draw.line ( screen ,green ,[0 ,0] ,[400 ,100] ,5)
    clock.tick(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True
Is it something wrong with IDLE, my drivers, my code?

  • Locked thread