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
Red Mike
Jul 11, 2011
code:
import shutil; shutil.copyfile(from_file,to_file)
:v:

Could even just use shutil.copy. On your example, leaving the file open should be fine. Python's smart enough to clean up its own objects when it's done, for one-liners like these. Just don't do it in a big script.

Adbot
ADBOT LOVES YOU

TasteMyHouse
Dec 21, 2006

Kimasu v2.0 posted:


Copy from one file to another. In the extra credit he says that he could do the whole program with one line of code. Well, I got the main part narrowed down to one line, but then I cannot figure out how to close the files.

Look up what a "with" statement is.

FoiledAgain
May 6, 2007

Kimasu v2.0 posted:

All of the documentation I can find on close() seems to need a variable assigned to it, as I tried to_file.close() but that didn't work. What am I missing? My code runs and works just fine, but I'm assuming it is leaving the file open, correct?

Maybe use with...as?
Something like
with open(to_file, 'w') as f: f.write(open(from_file).read())

Kimasu v2.0
Jan 19, 2001
Forum Veteran
Thanks for the instant feedback. I'm going to play around with the with statement and see what I can come up with.

Also, thanks for shutil. Always nice to find a single module to make my newbie code look like a waste of time :)

Reformed Pissboy
Nov 6, 2003

Thermopyle posted:

I'm not alone in my feeling that it's not very good:

quote:

Dive Into Python isn't just bad because of the use of ODBC, it's also just full of bad initial examples. Take a look at your first Python program and boggle at all the bizarre stuff a beginner has to suddenly comprehend:
...
I could see if Mr. Pilgrim had a giant disclaimer or something warning people that this isn't a beginner's book, but he doesn't.

Whatever point Zed might have there about that not being a particularly good first example is overshadowed by how disingenuous he's being. The first sentence on Dive Into Python's website says that it is for "experienced programmers," and right above the example he links to it says "It probably makes absolutely no sense to you. Don't worry about that, because you're going to dissect it line by line. But read through it first and see what, if anything, you can make of it." At no point is the reader required to suddenly comprehend anything and you would have to be deliberately obtuse to think otherwise.

Not to mention that his article is 90% stupid character assassination and ranting which is far more poisonous to programming culture than Pilgrim leaving old work online, and I guess my point is Zed Shaw doesn't deserve the attention I've just paid to him. His book is ``Okay`` though.

Thermopyle
Jul 1, 2003

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

Yay posted:

Even if you don't know who he is, you know you're citing Zed Shaw there, in a discussion of whether (Zed Shaw's) LPTHW is a self-congratulatory wank fest, or a better source than Dive into Python?

Hah! I didn't even notice the byline because I read that a long time ago and just googled up the link for it just for that post.


Transfatuation posted:

Whatever point Zed might have there about that not being a particularly good first example is overshadowed by how disingenuous he's being. The first sentence on Dive Into Python's website says that it is for "experienced programmers," and right above the example he links to it says "It probably makes absolutely no sense to you. Don't worry about that, because you're going to dissect it line by line. But read through it first and see what, if anything, you can make of it." At no point is the reader required to suddenly comprehend anything and you would have to be deliberately obtuse to think otherwise.

Not to mention that his article is 90% stupid character assassination and ranting which is far more poisonous to programming culture than Pilgrim leaving old work online, and I guess my point is Zed Shaw doesn't deserve the attention I've just paid to him. His book is ``Okay`` though.

I agree that it's full of way too much vitriol, but I'm not sure he's being completely disingenuous because up until 6 months ago Dive Into Python was what I saw recommended to beginners the vast majority of the time. In fact, for some reason, I've never seen Dive Into Python recommended for anyone but beginners. I'm probably a bit more sensitive to this than most people in this thread since I was nothing but a beginner 2 years ago and Dive Into Python was recommended to me all the loving time.

Regardless, I don't give a poo poo about Zed Shaw and whatever axe he has to grind...I think his points about Dive into Python being horrible for beginners is still valid.

Thermopyle fucked around with this message at 00:12 on Aug 12, 2011

tef
May 30, 2004

-> some l-system crap ->
eventually, zed will throw his arms up and yell 'python is a ghetto' and then he'll move on to trolling the javascript community

Gothmog1065
May 14, 2009
Okay, I asked a while back if there was a site that had small projects so I could work on learning Python after finishing the book thing I read. Someone mentioned Project Euler. The problem here is not only am I terrible at math, I'm not a good programmer yet. Not only that, but I'm so pathetic that problem 3 left me wanting to curl up in a corner and cry.

So back to being a pathetic loser, is there a good site that doesn't rely heavily on math that I can go to, to help build my programming skills?

tripwire
Nov 19, 2004

        ghost flow

Gothmog1065 posted:

Okay, I asked a while back if there was a site that had small projects so I could work on learning Python after finishing the book thing I read. Someone mentioned Project Euler. The problem here is not only am I terrible at math, I'm not a good programmer yet. Not only that, but I'm so pathetic that problem 3 left me wanting to curl up in a corner and cry.

So back to being a pathetic loser, is there a good site that doesn't rely heavily on math that I can go to, to help build my programming skills?

I'd recommend Project Euler.

Gothmog1065
May 14, 2009

tripwire posted:

I'd recommend Project Euler.

You enjoy making people cry don't you?

duck monster
Dec 15, 2004

Honestly I learned my python by just doing the official tutorial. Its actually a brilliant tutorial and covers everything you'll need for 99% of your needs. Beyond that, just read the module docs and if you have to deal with any esoteria like lambdas or whatever, just hit up google, or better still just ignore it. I've been hacking away at python for over a decade and some features I've almost *never* used simply because I don't like the syntax for them (Ie , I cant stand the "x = foo if condition else bar" syntax. It just looks crap to me, so I dont use it. (I tend to value readability over compactness, and those things just look poo poo to me.)

To be honest I did it in an afternoon. Python was a bit more sparse in the 1.x days.

duck monster fucked around with this message at 06:51 on Aug 12, 2011

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



I had no experience with Python when I started my current job, but the syntax is readable and easy to follow, so when I needed expand a colleague's script, I just looked up the docs and changed it.

That's pretty much how I've learned most languages, by having some source code, and needing it to do something slightly different, so looking up documentation and hacking at it until it did what I wanted. After a while, you can just start writing your own from scratch. My early scripts look terrible, but they got the job done.

This week I wrote a python script that output a shell script that used a bunch of old perl scripts I wrote when I first started. The output from the perl scripts was 5 million lines of SQL :pwn: (worked as intended)

Capnbigboobies
Dec 2, 2004

Gothmog1065 posted:

Okay, I asked a while back if there was a site that had small projects so I could work on learning Python after finishing the book thing I read. Someone mentioned Project Euler. The problem here is not only am I terrible at math, I'm not a good programmer yet. Not only that, but I'm so pathetic that problem 3 left me wanting to curl up in a corner and cry.

So back to being a pathetic loser, is there a good site that doesn't rely heavily on math that I can go to, to help build my programming skills?

Just try to think of simple projects. The first thing I ever did that was useful in python was make a script that generated a podcast rss from an mp3 that got downloaded off sabnzbd+. I thought it would be really hard, but after like a hundred lines I was done. Felt great to actually make something outside the crappy examples in books.

Also you could make one-off scripts for stuff that you find useful.

I know people hate the "Learn Python the Hard way" guy, but really after reading it python finally clicked for me.

After many false starts trying to learn any program language I am excited that I am able to actually DO things in python. I am working on a RSS reader and before that would of seemed like an impossible dream.


Hey what would people recommend to make gui apps in python? wxPython?

Capnbigboobies fucked around with this message at 07:50 on Aug 12, 2011

TasteMyHouse
Dec 21, 2006

Gothmog1065 posted:

Okay, I asked a while back if there was a site that had small projects so I could work on learning Python after finishing the book thing I read. Someone mentioned Project Euler. The problem here is not only am I terrible at math, I'm not a good programmer yet. Not only that, but I'm so pathetic that problem 3 left me wanting to curl up in a corner and cry.

So back to being a pathetic loser, is there a good site that doesn't rely heavily on math that I can go to, to help build my programming skills?

seconding project Euler. Don't give up on it. there's no shame in looking things up until you understand them. A lot of the math on project Euler is somewhat esoteric but the third question sure isn't, and if you aren't comfortable enough with math to answer that question it is definitely going to bite you in the rear end later.

I learned to program with project Euler. I remember it took a bit before I had a solution to question three that was efficient and readable, but thinking about the problem really solidified a lot of basic programming stuff in my head, the most important of which was taking a problem in a domain that I wasn't familiar with, breaking it down, and implementing a solution. If you do any real programming, this is probably the most important skill to have (behind refactoring, source control, and the ability to not freak out at incompetent coworkers)

Gothmog1065
May 14, 2009
I'll keep at it then. This is just a basic part time thing going for me right now, and it's hard to find something to code when there's no real need for it. Thanks for the input!

FoiledAgain
May 6, 2007

Gothmog1065 posted:

This is just a basic part time thing going for me right now, and it's hard to find something to code when there's no real need for it.

This is exactly what stopped me from learning to program years ago. I would open up books or tutorials and I would quit because I didn't care about looking up fictitious employee phone numbers or whatever. What really got me into things was having a project. That's the most important motivator really - choose a project, no matter how large, and then learn whatever you need to finish it. You'll learn piecemeal, some times hitting really advanced things early on, but as long as you care to finish your project you'll come out a better programmer. This might be a good start: is there something you have to do everyday that you wish was automated? Find a way to make it so.

Gothmog1065
May 14, 2009

FoiledAgain posted:

This is exactly what stopped me from learning to program years ago. I would open up books or tutorials and I would quit because I didn't care about looking up fictitious employee phone numbers or whatever. What really got me into things was having a project. That's the most important motivator really - choose a project, no matter how large, and then learn whatever you need to finish it. You'll learn piecemeal, some times hitting really advanced things early on, but as long as you care to finish your project you'll come out a better programmer. This might be a good start: is there something you have to do everyday that you wish was automated? Find a way to make it so.

You know, this might work. I do have a fairly large project I wanted to do for shits and giggles. The small computer shop I worked for needs a inventory/client DB program. It seemed too large for me right now, but I might just start climbing that mountain and learn it bit by bit. Thanks again for the input!

TasteMyHouse
Dec 21, 2006

Gothmog1065 posted:

You know, this might work. I do have a fairly large project I wanted to do for shits and giggles. The small computer shop I worked for needs a inventory/client DB program. It seemed too large for me right now, but I might just start climbing that mountain and learn it bit by bit. Thanks again for the input!

I would strongly recommend against actually deploying something you build while you're in this stage of learning in a business environment, especially something complex like that. if you're just using that as inspiration and don't actually deploy it this is a good idea.

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES

FoiledAgain posted:

This is exactly what stopped me from learning to program years ago. I would open up books or tutorials and I would quit because I didn't care about looking up fictitious employee phone numbers or whatever. What really got me into things was having a project. That's the most important motivator really - choose a project, no matter how large, and then learn whatever you need to finish it. You'll learn piecemeal, some times hitting really advanced things early on, but as long as you care to finish your project you'll come out a better programmer. This might be a good start: is there something you have to do everyday that you wish was automated? Find a way to make it so.

I generally follow the 10 minute rule for coding. If it's going to take more than 10 minutes to do, and I'll likely have to do it again, I'm going to write a code to do it. It may take me all day to write that code, but in the end, I'll never have to do that task again.

Then again, a lot of my work is doing the same poo poo over and over, so any time someone says "oh this will take an hour", I think to myself how many times I'm going to have to repeat that hour if I don't just automate it.

Gothmog1065
May 14, 2009

TasteMyHouse posted:

I would strongly recommend against actually deploying something you build while you're in this stage of learning in a business environment, especially something complex like that. if you're just using that as inspiration and don't actually deploy it this is a good idea.

Trust me, I'm not stupid enough to deploy something I don't know works properly. The most "deployment" would be on my own computers. It would be more of a pet project to help learn.

little munchkin
Aug 15, 2010
[quote = Gothmog1065]Okay, I asked a while back if there was a site that had small projects so I could work on learning Python after finishing the book thing I read. Someone mentioned Project Euler. The problem here is not only am I terrible at math, I'm not a good programmer yet. Not only that, but I'm so pathetic that problem 3 left me wanting to curl up in a corner and cry.

So back to being a pathetic loser, is there a good site that doesn't rely heavily on math that I can go to, to help build my programming skills?[/quote]

You can type this into google and find tons of stuff if you're willing to click around a little:

quote:

computer science "topic" lab site:*.edu
Change "topic" to whatever want you learn about, and you can change lab to homework if you want lengthier problems.

tef
May 30, 2004

-> some l-system crap ->

Gothmog1065 posted:

I'll keep at it then. This is just a basic part time thing going for me right now, and it's hard to find something to code when there's no real need for it. Thanks for the input!

Project Euler is a little programming, and a lot of discrete math.

Really, you just need to write a whole bunch of code, write yourself into a corner and go 'oh crap'. The only way to learn is to make mistakes and learning from them. You need to worry about the latter way more than the former.

I would suggest picking small, weekend tasks where you can get something that works as quickly as possible, and with plenty of time for polish. If you take on something huge, it is difficult to know how to structure the problem, let alone begin attacking it. It is easier to get a small prototype to play with and iterate it heavily than to design everything up front.

That said, there are some 'classic' weekend projects i've seen a bunch of people do:

RSS Reader - Try writing a parser/aggregator for rss feeds (like 'planet')
IRC Bot - The protocol is relatively straight forward, human readable and easy to test

If you've got experience with unix, another task you can do is to re-write command line tools in python to get an understanding for the apis involved - try writing cat, sort, ls, grep, find and netcat

The other important thing is to read more code. Picking up a large powerful library and trying to do things with it will help significantly more than trying to create from scratch.

For example, PyQt/PySide is quite powerful and it is relatively easy to write a simple webbrowser inside of it based around webkit http://pastie.org/private/fvrjpb9yp15bvrpmpdopiq

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"

tef posted:

RSS Reader - Try writing a parser/aggregator for rss feeds (like 'planet')
yo, speaking thereof, sn.printf.net is still not parsing my feed correctly.

I don't think it's a problem on my end because google reader works fine, but if you need me to change something in the server let me know

tef
May 30, 2004

-> some l-system crap ->

Janin posted:

yo, speaking thereof, sn.printf.net is still not parsing my feed correctly.

I don't think it's a problem on my end because google reader works fine, but if you need me to change something in the server let me know

hmm. well I thought it was just a broken url, but planet seems to hate https urls :-(

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"

tef posted:

hmm. well I thought it was just a broken url, but planet seems to hate https urls :-(
Is it giving you any errors / stack traces in the logs? Supposedly Planet uses Mark Pilgrim's feed parser, which handles https fine:

code:
>>> import feedparser
>>> d = feedparser.parse("https://john-millikin.com/feed/")
>>> d.feed.link
u'https://john-millikin.com/'
I don't wanna slum in the non-https ghetto again :(

Jewel
May 2, 2009

No idea if this is the right place, but I'm going to ask here since it's a little more popular than the general Game Development thread, but the GameDev thread might know how to handle graphics better.

Either way, I've been trying to get a general land/noise generation thing going, and since python handles arrays super easily I chose to use it for prototyping it.

I haven't done much like this before but I've always wanted to so I just whipped this up in a few minutes after looking at this example: http://properundead.com/2009/03/cave-generator.html (scroll down a bit to see)

I tried using the same test-data that he used in the article (shown at the bottom of this post) but I can't get anything remotely close to his, but mine instead ends up more speckled. I can't really think what could be wrong.

My code: http://pastebin.com/9Hrt0diR

Another thing. I'm such a sloppy coder all the time, and I'd really really like someone to tell me where I could improve in my code, since I tend to get messy when I use python due to the freedom it gives me, and even more trouble in an environment that uses pointers.

Picture of what mine does (top) and what I want it to roughly do (bottom):



Edit:

Hell yes I fixed it. I changed this code:

code:
lc = adj.count(True)*25
randchance = random.randint(0,100)
            
tmp.append(randchance<=lc)
to:

code:
if lc>50:
    tmp.append(True)
elif lc<50:
    tmp.append(False)
else:
    tmp.append(random.randint(0,1)==True)
And now it makes this:


A funny thing is, I changed the code to this accidentally:

code:
if lc>50:
    tmp.append(True)
else:
    tmp.append(False)
And it made this wonderful accident:


Either way, I'd still enjoy it if people could show me better ways to just write in python in general now that the graphic-generation part is out of the way!

Jewel fucked around with this message at 12:48 on Aug 14, 2011

Red Mike
Jul 11, 2011
That's actually a good article for reference on something I'm going to need, thanks.

About improving your code, I'd first say limiting your try: except: and handling it without exceptions yourself in those cases. Like, for example, making sure you're NOT referencing tiles that are outside the grid, or if you are, add an if 0<x<width and 0<y<width: before-hand. Try statements are pretty taxing, and chaining them only adds to the problem.

Besides that, I can see that it's sloppy code, but I wouldn't know where to begin, past doing it all more OOP-style. But for prototypes, that doesn't always matter.

raymond
Mar 20, 2004
You could read the PEP8 style guide for some tips about naming conventions and spaces around operators. http://www.python.org/dev/peps/pep-0008/

I would also do it more OOP style as Red Mike mentioned. The Grid could be a class with different methods on it, like __init__, split, draw, etc.

Opinion Haver
Apr 9, 2007

I'm writing some sqlalchemy code that involves doing a lot of filters and joins and such, and so I typically wind up doing stuff like

code:
results = session.query(Foo).join(Bar, Baz).filter((Foo.id == 12345) & (Bar.id > 54321)).all()
What's considered a proper style for a line break location? I've been putting them just before the dots and using line continuations.

Gothmog1065
May 14, 2009

tef posted:

For example, PyQt/PySide is quite powerful and it is relatively easy to write a simple webbrowser inside of it based around webkit http://pastie.org/private/fvrjpb9yp15bvrpmpdopiq
Blah, no 3.x libraries out yet. Going to look through it either way though.

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

yaoi prophet posted:

I'm writing some sqlalchemy code that involves doing a lot of filters and joins and such, and so I typically wind up doing stuff like

code:
results = session.query(Foo).join(Bar, Baz).filter((Foo.id == 12345) & (Bar.id > 54321)).all()
What's considered a proper style for a line break location? I've been putting them just before the dots and using line continuations.

In your case specifically, I would write it like this:
code:
results = session.query(Foo).join(Bar, Baz).filter(
    (Foo.id == 12345) & (Bar.id > 54321)
).all()
I'm personally not a fan of line continuations, and try not to use them very much, preferring to take advantage of line breaks within parentheses wherever possible. If there was no decent place to break in this way, I would probably restructure the code a bit, using a temp variable or something.

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES

Gothmog1065 posted:

Blah, no 3.x libraries out yet. Going to look through it either way though.

Things like this are why I've never even used 3.x... It seems like it's going to be a looong time before 3.x really gets caught up to by all the best libraries.

Red Mike
Jul 11, 2011

yaoi prophet posted:

I'm writing some sqlalchemy code that involves doing a lot of filters and joins and such, and so I typically wind up doing stuff like

code:
results = session.query(Foo).join(Bar, Baz).filter((Foo.id == 12345) & (Bar.id > 54321)).all()
What's considered a proper style for a line break location? I've been putting them just before the dots and using line continuations.

Line continuations make for an annoying read sometimes. I generally try to use the paranthesis implicit continuation, but also try to group lines logically, so your code I'd write like:

code:
results = session.query(Foo)
results = results.join(Bar,Baz).filter((Foo.id == 12345) &
    (Bar.id > 54321))
results = results.all()
This isn't really logically grouped, but in a different example it'd make more sense. Either way, when it seems like a line becomes hard to read because it's all on a line, split it on multiple ones. Don't sacrifice that much readability to keep it all 'concise', because then your code becomes sloppy when you pause in development and come back later.

FredMSloniker
Jan 2, 2008

Why, yes, I do like Kirby games.
I just decided to take a look at this thread because I'm pondering learning a programming language besides BASIC and writing a game with it. However, the OP hasn't been updated since April 2010, and the guy who started the thread hasn't been around September 2010. Anyone willing to volunteer to start a new thread so we can get an up-to-date OP?

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

FredMSloniker posted:

I just decided to take a look at this thread because I'm pondering learning a programming language besides BASIC and writing a game with it. However, the OP hasn't been updated since April 2010, and the guy who started the thread hasn't been around September 2010. Anyone willing to volunteer to start a new thread so we can get an up-to-date OP?

What exactly needs to be updated? Looks like everything it needs is there.

Lurchington
Jan 2, 2003

Forums Dragoon
I'd tend to agree, for the crew just getting into it, there's not really a whole lot that would need to be updated. m0nk3yz may not be posting a lot, but I'm not sure this is the kind of thread in need of constant OP updates


VVV it's not great form to cherry-pick the phrase where I'm conceding your point while omitting the actual heart of what I said :p

Lurchington fucked around with this message at 17:39 on Aug 18, 2011

FredMSloniker
Jan 2, 2008

Why, yes, I do like Kirby games.

LuckySevens posted:

What exactly needs to be updated?

Well, I wouldn't know, being new to Python. :v:

Lurchington posted:

m0nk3yz may not be posting a lot

I think not posting since September 29, 2010 constitutes a bit more than 'not posting a lot'. Even if nothing in the OP needs to be changed (and there is at least one change that even my newbie eyes can see: Python's now on versions 2.7 and 3.2), it'd be good to have it in the hands of someone who can make any necessary changes.

That said, if nobody wants to volunteer to do it, welp.

TasteMyHouse
Dec 21, 2006

FredMSloniker posted:

Well, I wouldn't know, being new to Python. :v:


I think not posting since September 29, 2010 constitutes a bit more than 'not posting a lot'. Even if nothing in the OP needs to be changed (and there is at least one change that even my newbie eyes can see: Python's now on versions 2.7 and 3.2), it'd be good to have it in the hands of someone who can make any necessary changes.

That said, if nobody wants to volunteer to do it, welp.

m0nk3yz has been going through some stuff recently. He'll be back. Things are fine right now.

spankweasel
Jan 4, 2006

TasteMyHouse posted:

m0nk3yz has been going through some stuff recently. He'll be back. Things are fine right now.

Yeah, like organizing Pycon 2012 ... be patient folks.

Adbot
ADBOT LOVES YOU

Profane Obituary!
May 19, 2009

This Motherfucker is Dead

spankweasel posted:

Yeah, like organizing Pycon 2012 ... be patient folks.

m0nk3yz is organizing Pycon '12?

  • Locked thread