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
Stabby McDamage
Dec 11, 2005

Doctor Rope

shrughes posted:

I spent much of my former job doing exactly this.

...

Lacking access to this, I'd just use IMAP, which I'm sure Python would have no trouble with.

Adbot
ADBOT LOVES YOU

king_kilr
May 25, 2007

Stabby McDamage posted:

Lacking access to this, I'd just use IMAP, which I'm sure Python would have no trouble with.

http://docs.python.org/library/imaplib.html

Thermopyle
Jul 1, 2003

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

I've been putting off learning web programming because it seemed like it was a completely different ballgame.

I mean, I decided to learn Python a year or 18 months ago, and while Python is completely awesome, the general ideas weren't much different from when I did lots of QuickBasic programming back in the early 90's.

My impression of web programming was that it was a lot different, and I didn't have a firm grasp on how all the pieces fit together. That coupled with the fact that I find any sort of HTML/CSS unfun, just put me off of the whole idea.

Well yesterday, I took the plunge and installed Django and started working through the tutorials.

I should have done this ages ago! As you can tell, I can't compare it to any other web frameworks, but man...Django is sweet stuff.

Just though I'd throw that out there for any other newbish programmers.

buglord
Jul 31, 2010

Cheating at a raffle? I sentence you to 1 year in jail! No! Two years! Three! Four! Five years! Ah! Ah! Ah! Ah!

Buglord
I've been working with Python for the first time today, and already it feels alot easier than C++ was when I tried starting out with that. Im having an issue though, not sure if its tied to the Python program.

Say write the print("Hello, World!") line in IDLE and save it as hello.py The output is supposed to show up on a command line-like screen. Well, it does, but only for a quarter of a second, then it dissapears. I have to relaunch the hello.py program many times just to make out what I'm seeing (obviously "Hello World", but in later lessons its not so easy to see everything.

Ideas?

Jehde
Apr 21, 2010

Avocadoes posted:

I've been working with Python for the first time today, and already it feels alot easier than C++ was when I tried starting out with that. Im having an issue though, not sure if its tied to the Python program.

Say write the print("Hello, World!") line in IDLE and save it as hello.py The output is supposed to show up on a command line-like screen. Well, it does, but only for a quarter of a second, then it dissapears. I have to relaunch the hello.py program many times just to make out what I'm seeing (obviously "Hello World", but in later lessons its not so easy to see everything.

Ideas?

Just hit F5 in IDLE, and it should run in the controlled environment Python shell.

Failing that, you could tack a raw_input() onto the end of your code and it'll wait until you hit Enter before it terminates the program.

Edit for more detail: When a Python program is run in a standard console, the console closes as soon as the program is finished. So with basic programs, it'll quickly compute everything, and the program will be finished, closing the console window. So you want to make it wait on something, ideally: Human input. So that you, the user, control when it finishes, and thus control when the console window closes. Easiest way to do this is to put the line "raw_input()" where you want it to wait.

Jehde fucked around with this message at 03:16 on Sep 28, 2010

Kgummy
Aug 14, 2009
I believe there's an option somewhere that is basically 'close program when finished'. Uncheck that. Alternatively, add in a user input segment, without actually saving what is inputted.

Don't remember off the top of my head, mostly because I'm just starting off myself.

buglord
Jul 31, 2010

Cheating at a raffle? I sentence you to 1 year in jail! No! Two years! Three! Four! Five years! Ah! Ah! Ah! Ah!

Buglord
I keep getting an invalid syntax error when plugging in like this

print("Hello, World!") raw_imput()

Maybe I followed your directions wrong. I'm illiterate to python still. And Kgummy I remember seeing that SOMEWHERE. It escapes me where I could find it though.

king_kilr
May 25, 2007
basically raw_input needs to be on the next line, it's a function of it's own.

Jehde
Apr 21, 2010

Avocadoes posted:

I keep getting an invalid syntax error when plugging in like this

print("Hello, World!") raw_imput()

Maybe I followed your directions wrong. I'm illiterate to python still. And Kgummy I remember seeing that SOMEWHERE. It escapes me where I could find it though.
Python is whitespace sensitive, so raw_input() needs to be on its own line.

Lurchington
Jan 2, 2003

Forums Dragoon
m0nk3yz is probably too humble to say so, but in this year's PyCon call for proposals it lists him as co-chair. Congrats and good luck. :)

Captain Capacitor
Jan 21, 2008

The code you say?
Congrats, BPTSFL!*

*Benevolent Python Thread Starter for Life

king_kilr
May 25, 2007

Lurchington posted:

m0nk3yz is probably too humble to say so, but in this year's PyCon call for proposals it lists him as co-chair. Congrats and good luck. :)

You guys better submit kick rear end talk proposals.

Freakus
Oct 21, 2000
I've been learning python recently through trying to write some personal projects. I've tried to search for best practices, but I couldn't find much with respect to module layout. One thing I've noticed is that in a class-per-file model that I'm used to using seems a bit redundant. E.g., if I had a Bar class in the file /foo/bar.py, when using it, I would:

from foo.bar import Bar

Is this redundancy normal? Or are there different ways people tend to accomplish this? I guess I could have it all in foo.py, in which case I could:

from foo import Bar

Of course, as foo.py got too large, I'd probably want to split it up.

Ferg
May 6, 2007

Lipstick Apathy

Freakus posted:

I've been learning python recently through trying to write some personal projects. I've tried to search for best practices, but I couldn't find much with respect to module layout. One thing I've noticed is that in a class-per-file model that I'm used to using seems a bit redundant. E.g., if I had a Bar class in the file /foo/bar.py, when using it, I would:

from foo.bar import Bar

Is this redundancy normal? Or are there different ways people tend to accomplish this? I guess I could have it all in foo.py, in which case I could:

from foo import Bar

Of course, as foo.py got too large, I'd probably want to split it up.

Sometimes you might see an implementation in the __init__.py so that you can just do
code:
import foo
I have no idea if this is frowned upon or what though. I just encountered it in a few projects and found it to be occasionally handy when you want to keep a module really simple and easy to import.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Lurchington posted:

m0nk3yz is probably too humble to say so, but in this year's PyCon call for proposals it lists him as co-chair. Congrats and good luck. :)

More like too tired and spinning in too many directions :( But yeah, what he said!

QuarkJets
Sep 8, 2008

I'm not sure if this is a short question or not... but here goes. I'm a physicist, and coding is generally something we do and not something that we're taught, so I'm hoping that someone with more knowledge of the tools available can tell me whether there's a better way to solve my problem.

I have two sets of run numbers, and each run number comes with a list of event numbers. I want to check overlap between the two runs so that I don't look at the same event twice, but I want to look at every event at least once. For example

RunStream1
Run 152000 has event numbers (1, 2, 3, 41, 45)
Run 152234 has event numbers (1, 2, 14, 15, 20)
.....

RunStream2
Run 152000 has event numbers (30, 31, 32, 34, 45)
Run 156000 has event numbers (blah blah blah)
.....

A single RunStream is guaranteed to not have run/event duplicates, but there is no such guarantee between runstreams; in this short example, run 152000 event 45 is in both sets. I have upwards of billions of events distributed cross hundreds of runs, so checking run/event overlap between the two runstreams becomes a pretty large task. I'm working on a server, not a supercomputer, so memory limitations are a concern.

Right now for overlap checking I use a dictionary where the run numbers determine the keys and the value of each key is a set() filled with event numbers. I only fill the dictionary/sets when looking at RunStream1, and I only check for overlap when looking at RunStream2. Dictionaries and sets are both hash lists, so checking for membership is fast and easy, as is appending new runs and events.

Is there a more effective way that might use less memory while maintaining at least the speed of a hash list?

chinchilla
May 1, 2010

In their native habitat, chinchillas live in burrows or crevices in rocks. They are agile jumpers and can jump up to 6 ft (1.8 m).
I'm quite the novice, and self-educated, so apologies if this is some blindingly simple error. That said...

code:
1 import media
2
3 pic = media.load_picture('C:\\pic.jpg')
4
5 for p in media.get_pixels(pic):
6     color_old = media.get_color(p)
7     color_new = media.lighten(color_old)
8     media.set_color(p, color_new)
9    
10 media.show(pic)
Python 2.5 with the pygraphics module. I keep getting this error message:

code:
>>> import lighten
Traceback (most recent call last):
  File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 1, in <module>
    # Used internally for debug sandbox under external interpreter
  File "C:\lighten.py", line 8, in <module>
  File "C:\Python25\Lib\site-packages\pygraphics\media.py", line 212, in set_color
    pix.set_color(col)
  File "C:\Python25\Lib\site-packages\pygraphics\pixel.py", line 94, in set_color
    self.set_red(color.get_red())
AttributeError: 'NoneType' object has no attribute 'get_red'
That's line 8 in my script, the media.set_color function. That function's at 212 in the media.py file:

code:
209 def set_color(pix, col):
210     '''Set the RGB values of Pixel pix to those of Color col.'''
211 
212     pix.set_color(col) 
Which isn't exactly helpful. So jump to line 94 of the pixel module:

code:
def set_color(self, color):
    '''Set the color values of this Pixel to those of Color object color.'''
        
    self.set_red(color.get_red())
    self.set_green(color.get_green())
    self.set_blue(color.get_blue())
Ok. So my script puts a pixel's original color in color_old. Presumably it makes that variable a Color object - if it were red it'd be Color(255,0,0). I assume that's working because the next line, the media.lighten function, isn't returning any error.

But the error message says "'NoneType' object has no attribute 'get_red'" and in that last code box, it's trying to "get_red" from my color_new object. So I guess lighten isn't returning a color-type object?

Or am I completely on the wrong track?

chinchilla fucked around with this message at 04:08 on Sep 30, 2010

tef
May 30, 2004

-> some l-system crap ->
if/when you run out of memory - write your program to work out of core by operating on files instead of python data structures

ie read the inputs and create/append to files called data/152/000.event
where each file contains some lines like:

000 runstream1: 1 2 3 41 45
000 runstream2: 1 2 14 15 20
234 runstream1: 30 31 32 34 45
blah blah blah


then, run your existing program over these subsets and write back
out to these files with the reduced event numbers

create a third program that reads these event files back
and spits out the range you want

tef
May 30, 2004

-> some l-system crap ->

chinchilla posted:

8lor(col) [/code]

Which isn't exactly helpful.


media.lighten(color_old) changes the value of colour_old and returns None

chinchilla
May 1, 2010

In their native habitat, chinchillas live in burrows or crevices in rocks. They are agile jumpers and can jump up to 6 ft (1.8 m).
Right. I guess that would do it. Thanks!

Habnabit
Dec 30, 2007

lift your skinny fists like
antennas in germany.

Freakus posted:

I've been learning python recently through trying to write some personal projects. I've tried to search for best practices, but I couldn't find much with respect to module layout. One thing I've noticed is that in a class-per-file model that I'm used to using seems a bit redundant. E.g., if I had a Bar class in the file /foo/bar.py, when using it, I would:

from foo.bar import Bar

Is this redundancy normal? Or are there different ways people tend to accomplish this? I guess I could have it all in foo.py, in which case I could:

from foo import Bar

Of course, as foo.py got too large, I'd probably want to split it up.

Nobody does class-per-file. This isn't java.

Organize your classes into modules in a logical grouping. Like classes and functions go together.

tripwire
Nov 19, 2004

        ghost flow
Thats one of the things I like about python. A folder is a package, a file is a module, and a class sits inside a module.

UberJumper
May 20, 2007
woop
Might be a really dumb question, is there anyway to build a python package from source, on windows when i don't have visual studio 2003? Or mingw? I have visual studio 2008, and its the only version i can get my hands on. I looked around online but couldn't find anything definate. Generally i like to just stick to the precompiled binaries to get around this headache, however i don't have that option for this library (http://effbot.org/media/downloads/ftpparse-1.1-20021124.zip).

I can't get my hands on visual studio 2003. If i were to use mingw, would the egg be portable to another computer where mingw is not installed, or would i have to install mingw on that computer?

tripwire
Nov 19, 2004

        ghost flow
You could use cygwin.

notMordecai
Mar 4, 2007

Gay Boy Suicide Pact?
Sucking Dick For Satan??

So this is my first post in here so try to be gentle. :(

Comp Sci major going into Java Development as a job (already have a job lined up). Most of the stuff I am used to is basic code monkey stuff, which is okay, but I am in a Python/Pylons dev class and have been asked to create my own personal "anything" as long as it uses Python.

One of the things that came to my mind to create was a simple time killer (others are creating actual applications such as twitter updating from cmd, creating tables by grabbing data from websites [such as weather, allergens], etc) but I wanted to create something more of a casual thing.

I had seen a website years ago where a guy created a website where a ball existed in the browser window and if the user moved the window in the OS, the ball reacted as if if the 2D plane had physics. Ergo, shaking the window thus shook the ball around and you could bounce it around.

Could this be replicated in python? If not in browser, maybe a standalone application using a GUI such as tkinter? I kind of want to ask more advanced folks as yourself if this seems feasible as a beginner project or if it seems like too of an advanced application and I should aim lower.

I should also add that the only python experience I have are creating simple applications that used basic arithmetic and other basic functions like sorting, etc... :3:

Freakus
Oct 21, 2000

Habnabit posted:

Nobody does class-per-file. This isn't java.

Organize your classes into modules in a logical grouping. Like classes and functions go together.

I don't necessarily do class per file, but it seems like in most modules I write there tends to be 1 class that is essentially named after the module. Maybe I'm just not grouping my classes and functions by high enough level concepts.

UberJumper
May 20, 2007
woop

tripwire posted:

You could use cygwin.

Turns out setting up mingw for python is insanely painless now:

http://www.develer.com/oss/GccWinBinaries

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

notMordecai posted:

So this is my first post in here so try to be gentle. :(

Comp Sci major going into Java Development as a job (already have a job lined up). Most of the stuff I am used to is basic code monkey stuff, which is okay, but I am in a Python/Pylons dev class and have been asked to create my own personal "anything" as long as it uses Python.

One of the things that came to my mind to create was a simple time killer (others are creating actual applications such as twitter updating from cmd, creating tables by grabbing data from websites [such as weather, allergens], etc) but I wanted to create something more of a casual thing.

I had seen a website years ago where a guy created a website where a ball existed in the browser window and if the user moved the window in the OS, the ball reacted as if if the 2D plane had physics. Ergo, shaking the window thus shook the ball around and you could bounce it around.

Could this be replicated in python? If not in browser, maybe a standalone application using a GUI such as tkinter? I kind of want to ask more advanced folks as yourself if this seems feasible as a beginner project or if it seems like too of an advanced application and I should aim lower.

I should also add that the only python experience I have are creating simple applications that used basic arithmetic and other basic functions like sorting, etc... :3:

Yes, that doesn't sound especially hard. It's pretty easy to get window coordinates and sizes in tkinter. Just start very simple and work your way up. Don't try to do the entire thing in one go. For example, first just render a window, then add a ball to the window, then make the ball move on its own, then figure out how to track the window position, and so on. None of this has anything to do with web development or browsers, though.

o.m. 94
Nov 23, 2009

This exercise takes a user-defined integer and prints it out in a "large ascii font", just wondering if someone could help out with the following:

a) Any suggestions to improve the code - make it more "pythonic"? The last language I used was like, C, in 2007.
b) Currently, each numeral displays on a new line, I'd like to get them all on the same line- any hints?

code:
Zero    = ["  ***  ", " *   * ", "*     *", "*     *", "*     *", " *   * ", "  ***  "]
One     = [" * ", "** ", " * ", " * ", " * ", " * ", "***"]
Two     = [" *** ", "*   *", "*  * ", "  *  ", " *   ", "*    ", "*****"]
Three   = [" *** ", "*   *", "    *", "  ** ", "    *", "*   *", " *** "]
Four    = ["   *  ", "  **  ", " * *  ", "*  *  ", "******", "   *  ", "   *  "]
Five    = ["*****", "*    ", "*    ", " *** ", "    *", "*   *", " *** "]
Six     = [" *** ", "*    ", "*    ", "**** ", "*   *", "*   *", " *** "]
Seven   = ["*****", "    *", "   * ", "  *  ", " *   ", "*    ", "*    "]
Eight   = [" *** ", "*   *", "*   *", " *** ", "*   *", "*   *", " *** "]
Nine    = [" ****", "*   *", "*   *", " ****", "    *", "    *", "    *"]

numbers = [Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine]

while True:
    try:
        user_input = int(input("Enter number: "))
    except ValueError as err:
        print(err)
        continue

    user_input = str(user_input)

    for i in user_input:
        for j in numbers[int(i)]:
            print(j)

o.m. 94 fucked around with this message at 12:34 on Oct 1, 2010

RobotEmpire
Dec 8, 2007
I haven't tested this, and I'm a novice myself, but it seems like you could do a dict like

{('0':numbers[0]), ('1':numbers[1]), ('2':numbers[2])} etc

then print the values using a generator function like print [value for key in dict]. I am on my way to work, was just thinking about it in the shower. Don't have time to test myself.

o.m. 94
Nov 23, 2009

RobotEmpire posted:

I haven't tested this, and I'm a novice myself, but it seems like you could do a dict like

{('0':numbers[0]), ('1':numbers[1]), ('2':numbers[2])} etc

then print the values using a generator function like print [value for key in dict]. I am on my way to work, was just thinking about it in the shower. Don't have time to test myself.

That's basically what I'm doing without a dict. I think the idea is to render each element of each number in the string into a "line" variable, and then go through, and render the whole thing bit by bit like an old dot matrix printer.

king_kilr
May 25, 2007

oiseaux morts 1994 posted:

This exercise takes a user-defined integer and prints it out in a "large ascii font", just wondering if someone could help out with the following:

a) Any suggestions to improve the code - make it more "pythonic"? The last language I used was like, C, in 2007.
b) Currently, each numeral displays on a new line, I'd like to get them all on the same line- any hints?

code:
Zero    = ["  ***  ", " *   * ", "*     *", "*     *", "*     *", " *   * ", "  ***  "]
One     = [" * ", "** ", " * ", " * ", " * ", " * ", "***"]
Two     = [" *** ", "*   *", "*  * ", "  *  ", " *   ", "*    ", "*****"]
Three   = [" *** ", "*   *", "    *", "  ** ", "    *", "*   *", " *** "]
Four    = ["   *  ", "  **  ", " * *  ", "*  *  ", "******", "   *  ", "   *  "]
Five    = ["*****", "*    ", "*    ", " *** ", "    *", "*   *", " *** "]
Six     = [" *** ", "*    ", "*    ", "**** ", "*   *", "*   *", " *** "]
Seven   = ["*****", "    *", "   * ", "  *  ", " *   ", "*    ", "*    "]
Eight   = [" *** ", "*   *", "*   *", " *** ", "*   *", "*   *", " *** "]
Nine    = [" ****", "*   *", "*   *", " ****", "    *", "    *", "    *"]

numbers = [Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine]

while True:
    try:
        user_input = int(input("Enter number: "))
    except ValueError as err:
        print(err)
        continue

    user_input = str(user_input)

    for i in user_input:
        for j in numbers[int(i)]:
            print(j)

Juse use print(j, end="") and then put a print outside the first loop.

Stabby McDamage
Dec 11, 2005

Doctor Rope
You could specify the digits much easier than that, without having to resort to converting them to an awkward list at the top.

code:
number_font = """
  ***  
 *   * 
*     *
*     *
*     *
 *   * 
  ***  
 * 
** 
 * 
 * 
 * 
 * 
***
 *** 
*   *
*  * 
  *  
 *   
*    
*****
 *** 
*   *
    *
  ** 
    *
*   *
 *** 
   *  
  **  
 * *  
*  *  
******
   *  
   *  
*****
*    
*    
 *** 
    *
*   *
 *** 
 *** 
*    
*    
**** 
*   *
*   *
 *** 
*****
    *
   * 
  *  
 *   
*    
*    
 *** 
*   *
*   *
 *** 
*   *
*   *
 *** 
 ****
*   *
*   *
 ****
    *
    *
    *
"""
height=7 # number of lines per digit
number_font_lines = number_font.split("\n")[1:] # shave off first line, which is empty
numbers = [number_font_lines[i*height : (i+1)*height]  for i in range(10)]

standardtoaster
May 22, 2009
I'm starting from square one in Python and programming.

Why is:

print 'this word'

valid in 2.7, but it's not in 3.1?

Reformed Pissboy
Nov 6, 2003

king_kilr posted:

Juse use print(j, end="") and then put a print outside the first loop.

That would print all the characters for an input digit on one line, which wouldn't be very readable. :)

Your end goal is to have something that accomplishes this:
code:
print( One[0], Two[0], Three[0] )
print( One[1], Two[1], Three[1] )
...
You can use the built-in function zip() to achieve this pretty easily. If you pass it some lists as arguments, it will return a list of tuples, where the ith tuple contains the ith element of each list. So, zip( [1,2,3] , ['a','b','c'] ) returns [(1,'a'), (2,'b'), (3,'c')]. That should get your data into order for printing quite nicely.

Opinion Haver
Apr 9, 2007

standardtoaster posted:

I'm starting from square one in Python and programming.

Why is:

print 'this word'

valid in 2.7, but it's not in 3.1?

Because in python 3.0 print is now a function:
print('this word')

You can get the print function in 2.7 by doing from __future__ import print_function (which will also remove the print statement).

Stabby McDamage
Dec 11, 2005

Doctor Rope
As stated earlier, you need to iterate on lines before you iterate on the user's digits. There certainly a more pythonic way to do this, but I just did it explicitly with two loops:

code:
while True:
	try:
		user_input = raw_input("Enter number: ")
		int(user_input) # just to check for exception
	except ValueError, err:
		print(err)
		continue
		
	for j in range(height): # for each row
		for i in user_input: # for each digit
			print numbers[int(i)][j], # print this line of the digit, then a space
		print # add newline
Also, you should probably use raw_input() instead of input(). The latter takes and evaluates python code, whereas the former just reads the string, which is what I assume you want.

EDIT: I had to use a comma instead of "as" in the except because I'm stuck on Python 2.5.

o.m. 94
Nov 23, 2009

Stabby McDamage posted:

Also, you should probably use raw_input() instead of input(). The latter takes and evaluates python code, whereas the former just reads the string, which is what I assume you want.

I'm using Python 3 - I thought input() was okay to use as raw_input() now?

Reformed Pissboy
Nov 6, 2003

Stabby McDamage posted:

As stated earlier, you need to iterate on lines before you iterate on the user's digits. There certainly a more pythonic way to do this, but I just did it explicitly with two loops:

snip

Haha derp, of course. Iterating over the height is way simpler and clearer than using zip. I like making things harder than they have to be apparently :shobon:

Adbot
ADBOT LOVES YOU

king_kilr
May 25, 2007

oiseaux morts 1994 posted:

I'm using Python 3 - I thought input() was okay to use as raw_input() now?

Yes, it is. raw_input doesn't exist anymore in 3.x

  • Locked thread