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
tef
May 30, 2004

-> some l-system crap ->

The Gripper posted:

For me as an old (young, but old at heart) and jaded programmer the only thing that keeps me interested is finding a new idea to work on. Most of my day-to-day work is maintenance, retrofitting, and tool design which isn't all that interesting or compelling any more.

Pick up a new hobby rather than practicing your day job in your free time. Learn to play an instrument perhaps.

quote:

Outside of work I just have an amazing case of writers block so I spend a lot of time reading about languages and design look for anything that is going to take some actual, solid research

Writers block is the process of editing the text before you've written it. Try writing stupid code instead of worrying what's right.

quote:

that isn't just referring to an API or picking up a new language without making something from it. 90% of the time I just want to sit down and code something but don't have the faintest idea of where to start.

This is probably because 90% of starting something new is playing with an api.

quote:

Currently that journey has taken me into the depths of 3D graphics/game engines, and I can't see myself coming out of this as a whiz-kid any time soon.

Does it matter what you learn now? If you're doing this in your free time, shouldn't it just be fun instead?

Adbot
ADBOT LOVES YOU

The Gripper
Sep 14, 2004
i am winner
I do play instruments!

It's not so much that I'm mentally writing something and tossing it out before it's down on paper, it that I get to the point where there's just nothing rattling around in there that I want to even start on. I've written a lot of stupid code and my github is scattered with small finished projects based on it, but that's mostly because I find it's the easiest way to pick up a new language or API (because it is!) and not because I've found some long-term engaging thing to work on more generally. It's a lot of fun picking up new languages but at some point it feels like I should find something more constructive to do!

tef posted:

Does it matter what you learn now? If you're doing this in your free time, shouldn't it just be fun instead?
Well that was what I was saying in that last paragraph, I find 3D graphics fun because it's such a massive change of pace and style that even small goals are rewarding, and it's a massive enough field that no matter what I do it's always going to have something else for me to mess around with in the future. I don't really get that elsewhere unless I have a great idea to work on (which I don't).

Bunny Cuddlin
Dec 12, 2004

tef posted:

https://gist.github.com/295200 you can use ctypes to monkeypatch cpython :eng99:

OH MAN I'm going to use this to monkeypatch the pygame joystick code to work with 360 controller triggers properly.

raminasi
Jan 25, 2005

a last drink with no ice

BeefofAges posted:

Are you sure you're passing it the right path? Are you giving it a relative path or an absolute path?
Yep, because it works in the majority of cases. Inevitably, though, one of the deletions will fail, bringing the whole program down, which is doubly annoying because the "failure" actually successfully deletes the directory - it just thinks it didn't. Which is why I thought ignoring the error would be helpful.

duck monster
Dec 15, 2004

Bunny Cuddlin posted:

OH MAN I'm going to use this to monkeypatch the pygame joystick code to work with 360 controller triggers properly.

Er, use the example I gave. What makes tef's example interesting is that your not *supposed* to be able to monkey patch builtin types.

Bunny Cuddlin
Dec 12, 2004

duck monster posted:

Er, use the example I gave. What makes tef's example interesting is that your not *supposed* to be able to monkey patch builtin types.

The joystick class in pygame is a C extension so you can't monkey patch it the normal way.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Jewel posted:

Did someone say, weird obtuse code that tests the boundaries of python and still works?

http://paste.pound-python.org/show/chatserver/

Daynab
Aug 5, 2008

everyone posted:


"Do or do not. There is no Try"
- Kurt Kobain 1943-1993

Heh, thanks for the tips. I'll keep trying and hopefully it will click soon.

Dren
Jan 5, 2001

Pillbug
The Gripper, I was also going to suggest getting yourself a not programming hobby like tef said.

Perhaps a not programming hobby will inspire you to write some apps to assist you with your hobby.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
The Gripper, doing this sort of maintenance work will kill a person's soul. You're not being challenged, intellectually.

What helped me a lot was visiting users of the software in the wild, and looking at their problems they have with the software (not just bugs, but workflow issues, places that looked inefficient), and solving their problems. I felt good about being a programmer that helped other people by applying his skills, and I stopped looking at it as "it's maintenance work", but "I'm helping all these people do their jobs, and without me, they couldn't work!"

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Suspicious Dish posted:

"I'm helping all these people do their jobs, and without me, they couldn't work!"

Sure they could :)

fritz
Jul 26, 2003

What's the fashionable way to do something like the following? I have a class A, and I want to wrap functionality of a class B. I can do something like:

code:
# within the body of A:
def func_1(self, ....):
	wrapper code
	B_holder = B(A)
	B_out = B_holder.func_1(...)
	wrapper code
	return 

but I'm going to have a bunch of these things, with identical wrapper code, and some people get antsy about that.

Should I have something like
code:

def __big_wrapper__(self, picker, ....):
	wrapper code	
	if picker == 'func_1': 
		B_out = B_holder.func_1(...)
	elif picker == 'func_2': 
		B_out = B_holder.func_2(...)
	etc

def func_1: 
	return self.__big_wrapper__('func_1',...)

etc
?

BeefofAges
Jun 5, 2004

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

GrumpyDoctor posted:

Yep, because it works in the majority of cases. Inevitably, though, one of the deletions will fail, bringing the whole program down, which is doubly annoying because the "failure" actually successfully deletes the directory - it just thinks it didn't. Which is why I thought ignoring the error would be helpful.

That's an odd problem. This sounds messy, but try catching the exception, checking to see if the delete actually worked, and only re-raising the exception if the delete failed?

Can you post a bit of your code so we can see what you're doing?

raminasi
Jan 25, 2005

a last drink with no ice

BeefofAges posted:

That's an odd problem. This sounds messy, but try catching the exception, checking to see if the delete actually worked, and only re-raising the exception if the delete failed?

Can you post a bit of your code so we can see what you're doing?

I think I actually figured it out. I was using a third-party component that wasn't behaving the way I thought it was: It was doing its own shutil shenanigans that were fighting with mine. I didn't see the problem because I don't know how to get exceptions usefully across the multiprocessing process boundary so all I knew was that shutil was involved somehow.

I really need a way to effectively debug code that uses multiprocessing.

The Gripper
Sep 14, 2004
i am winner

fritz posted:

What's the fashionable way to do something like the following? I have a class A, and I want to wrap functionality of a class B. I can do something like:

?
I have no idea what the fashionable thing would be to do, but it can be done with decorators if the wrapper code is always going to be the same, through something like:

Python code:
class SomeWrapper(object):
    """@SomeWrapper(class, "function")"""
    def __init__(self, c, f):
        self.c = c
        self.f = f

    def __call__(self, func):
        wrapper_self = self

        def actual_wrapper(*args, **kwargs):
            print "##PRE-RUN CODE HERE##"
            #func(*args) #you can call the original function if necessary
            getattr(wrapper_self.c(), wrapper_self.f)(*args[1:], **kwargs)

            print "##POST-RUN CODE HERE##"
        return actual_wrapper


class B:
    def func1(self, a, b, c):
        print "B::func1 %s %s %s" % (a, b, c)

class A:
    @SomeWrapper(B, "func1")
    def func1(self, a, b, c):
        print "A::func1 %s %s %s" % (a, b, c)


print "Calling original B.func1 (unwrapped)"
y = B()
y.func1(1, 2, 3)
print "---"
print "Calling A.func1 wrapping B.func1"
x = A()
x.func1(1, 2, 3)

Basically you decorate your function in the body of A with your wrapper, which performs any of the pre and post-wrapped logic and calls the wrapped function where needed. It's more complicated in general but it means you can wrap whatever you want by just decorating methods in other classes. If you wanted you could set the wrapper to take additional arguments like pre and post-run functions, if the logic isn't always the same for some flexibility.

Edit; i've heard bad things about decorator classes though, so it might be better to use a regular decorator method. Holy lord why did I just write out that giant class decorator one when a regular one is way more concise :downs:
Python code:
def mywrapper(c, f):
    def r_dec(function):
        def wrapper(*args, **kwargs):
            print "## PRE-RUN CODE HERE ##"
            getattr(c(), f)(*args[1:], **kwargs)
            print "## POST-RUN CODE HERE ##"
        return wrapper
    return r_dec
and decorate as per usual:
Python code:
"""in class A:"""
@mywrapper(B, "func1")
def func1(self, a, b, c):
   print "stuff"
https://gist.github.com/54b26d120ffb8a2412e2

The Gripper fucked around with this message at 06:56 on Dec 12, 2012

Vivian Darkbloom
Jul 14, 2004


Any recommendation for a GUI to use with pygame? The pygame wiki has some suggestions, but based on these reviews nothing seems very mature.

e: The stuff I want to do includes buttons, text boxes (preferably with scrolling), windows within the pygame window, etc. I could do this myself, I guess..

Vivian Darkbloom fucked around with this message at 08:57 on Dec 12, 2012

duck monster
Dec 15, 2004

Dren posted:

The Gripper, I was also going to suggest getting yourself a not programming hobby like tef said.

Perhaps a not programming hobby will inspire you to write some apps to assist you with your hobby.

Or even download pygame or cocos2d - python, do some tutorials and try making some nutty space invader game or something. Games are fun, just dont get too ambitious (the big games are in fact really big).

Incidently heres whats fun;-

http://kivy.org/

Python;- check. Gods own language.
loving easy to use UI framework;- check. Easiest non RAD framework I've seen.
Looks good;- check. You could portfolio stuff from this with just the defaults.
Cross platform;- double check. Toolkit will compile to IOS and Android.

For added fun, you can hatchet Panda3D into a widget for gaaaaames. Probably pygame too with a bit of twangling.

duck monster fucked around with this message at 12:12 on Dec 12, 2012

Sylink
Apr 17, 2004

Is there a cleverer way of representing grids in Python instead of just a 2D array/list?

I want to store data and be able to do something simple like grid[1][5] returns whatever value is at coordinate x:1,y:5 in my model.

The next thing I need is a quick way to return the surrounding points in the grid. So a point like the one above has 8 neighboring points surrounding it at (0,4),(1,6) etc.

I can do this using multi-dimensional arrays but if there is a module that deals specifically with grids like this and can do other things I could use that instead.

duck monster
Dec 15, 2004

Sylink posted:

Is there a cleverer way of representing grids in Python instead of just a 2D array/list?

I want to store data and be able to do something simple like grid[1][5] returns whatever value is at coordinate x:1,y:5 in my model.

The next thing I need is a quick way to return the surrounding points in the grid. So a point like the one above has 8 neighboring points surrounding it at (0,4),(1,6) etc.

I can do this using multi-dimensional arrays but if there is a module that deals specifically with grids like this and can do other things I could use that instead.

Doesn't numpy have a fairly versatile n-dimensional array?

Bunny Cuddlin
Dec 12, 2004

Sylink posted:

Is there a cleverer way of representing grids in Python instead of just a 2D array/list?

I want to store data and be able to do something simple like grid[1][5] returns whatever value is at coordinate x:1,y:5 in my model.

The next thing I need is a quick way to return the surrounding points in the grid. So a point like the one above has 8 neighboring points surrounding it at (0,4),(1,6) etc.

I can do this using multi-dimensional arrays but if there is a module that deals specifically with grids like this and can do other things I could use that instead.

There's numpy arrays and if you want even more fancy functionality (that also uses numpy arrays) you can try pandas dataframes.

Sylink
Apr 17, 2004

I ended up making my own class for it:

code:

class Grid:
	#2D grid class for tracking neighbors and other values
	#for use with Networkx module to generate nodes and edges
	def __init__(self,x_dimension,y_dimension):
		self.grid = []
		self.dimensions = x_dimension,y_dimension
		for y in range(0,y_dimension):
			self.grid.append([])
			for x in range(0,x_dimension):
				self.grid[y].append(str(y) + ',' + str(x))
	
	def neighbors(self,x,y):
		neighbors = []
		for a in range(-1,2):
			for b in range(-1,2):
				try:
					if x+a >= 0 and y+b >= 0:
						neighbors.append(self.grid[x+a][y+b])
				except IndexError:
					pass
		neighbors.remove(self.grid[x][y])
		return neighbors
		
		
		
	
	def __getitem__(self, key):
		return self.grid[key]

Maybe its not the best but it works and I need simple methods so I can work this in with another module to generate graph edges automatically.

Daynab
Aug 5, 2008

Oh man, after 2-3 days of struggling, not really being able to absorb any information or understanding, it clicked. I can make a basic GUI app, and self, inits, inheritence and all that stuff just kind of fell into place in my mind.

:unsmith:

Jewel
May 2, 2009

Daynab posted:

Oh man, after 2-3 days of struggling, not really being able to absorb any information or understanding, it clicked. I can make a basic GUI app, and self, inits, inheritence and all that stuff just kind of fell into place in my mind.

:unsmith:

Coding seems to do that! The feeling of things suddenly just.. clicking, is fantastic. Really gives you a good high, which you can then use for just coding nonstop for 8 hours hahah.

Optimus Prime Ribs
Jul 25, 2007

Daynab posted:

and self

Having to pass self to instance methods is probably what tripped me up the most while learning Python, but when I figured out why you actually do that it certainly helped. Now it just makes perfect sense and I don't even think about it when writing Python.

duck monster
Dec 15, 2004

Daynab posted:

Oh man, after 2-3 days of struggling, not really being able to absorb any information or understanding, it clicked. I can make a basic GUI app, and self, inits, inheritence and all that stuff just kind of fell into place in my mind.

:unsmith:

Like falling off a bike. You might forget syntax (Hell I forget syntax minutes after learning it, always), or how a certain thing works but the basic concept of programming once learned is never forgotten.

Welcome to the club.

Captain Capacitor
Jan 21, 2008

The code you say?


One of us
One of us
One of us

onionradish
Jul 6, 2006

That's spicy.
I could use some advice on project structure so I'm not kicking myself later.

I'm about to migrate to new machine and start a large personal project "for reals" with versioning, proper classes, etc. instead of the folder of hodgepodge scripts I've been using to learn. When I've done development in other languages, all the code and assets were contained within a single \\dev\projectname path. It was easy to manage dependencies because third-party scripts, modules, frameworks, etc. were revisioned along with the rest of the code.

This project will require some third-party modules (numpy, etc.). Since these get installed to Python's directory, how should I manage dependencies on these modules so I can re-create the development environment when I have to move machines, restore from backup, etc.? I can just keep copies of the module installation packages, but I'm guessing there are better ways to go about it.

The Gripper
Sep 14, 2004
i am winner

onionradish posted:

This project will require some third-party modules (numpy, etc.). Since these get installed to Python's directory, how should I manage dependencies on these modules so I can re-create the development environment when I have to move machines, restore from backup, etc.? I can just keep copies of the module installation packages, but I'm guessing there are better ways to go about it.
A lot of people use virtualenv since that lets you keep separate third-party modules for each of your projects, which is good if you ever need different versions of modules for different projects as well.

Combining that with `pip freeze > requirements.txt` will let you save a list of modules installed (and versions) for that project and reinstall those same modules wherever you go with `pip install -r requirements.txt`.

Cat Plus Plus
Apr 8, 2011

:frogc00l:

onionradish posted:

This project will require some third-party modules (numpy, etc.). Since these get installed to Python's directory, how should I manage dependencies on these modules so I can re-create the development environment when I have to move machines, restore from backup, etc.? I can just keep copies of the module installation packages, but I'm guessing there are better ways to go about it.

Before anything, get virtualenv.

One easy option is pip freeze. Install everything you need, call freeze, get a requirements file. You can later feed it to pip to reinstall everything in the exact same versions. Or write requirements file yourself. If you use distribute for you projects, you can also just specify dependencies in setup.py and simply install it to get everything.

There's also buildout, but I've never really used it, so can't tell you how easy it is to setup/use.

Haystack
Jan 23, 2005





What you want is virtualenv to isolate your dependencies and pip to download your dependencies.

Generally speaking, you want to keep your dependencies out of your version control. I usually have a folder for virtualenvs, and a folder for my projects. Each project in the projects folder has an outer directory (e.g. "FooProject") that contains a requirements text file (look up "pip freeze"), any other project metadata, and the actual python package that I'm developing (which is another directory with an __init__.py and my actual python code).

It looks roughly like this

code:
Work/
|-- projects
|   |-- FooProject
|   |   |-- .git
|   |   |-- foo_project
|   |   |   |-- __init__.py
|   |   |   |-- foo.py
|   |   |   |-- tests.py
|   |   |-- development.ini
|   |   |-- production.ini
|   |   |-- README.txt
|   |   |-- requirements.txt
|-- virtualenvs
|   |-- FooProject 
|   |   |-- ...Foo Project Environment Files...

Edit: Forgot an indent in the dirtree

Haystack fucked around with this message at 22:22 on Dec 13, 2012

onionradish
Jul 6, 2006

That's spicy.
Thanks for the advice; and, the sample dirtree is a big help, so thanks for that detail, Haystack! I'll need to do some more reading and testing on virtualenv as soon as I finish uninstalling all the bloatware that came with the new system.

In the example usage given in the docs:

code:
# This creates ENV/lib/pythonX.X/site-packages
$ python virtualenv.py ENV
Is ENV representative of a full path (C:\my_python_files\), a subdirectory of c:\Python27, or some alias that refers to a full path?

Also, when I'm installing modules/packages, do I install to a project's particular virtualenv, to the native Python directory or both? For example, if I want a module like numpy or PIL to be available to every script, I'm assuming that I install it to native Python directory. For my actual project, I'm assuming I'll also need to install it to its particular virtualenv so that version is the one that will run.

Cat Plus Plus
Apr 8, 2011

:frogc00l:

onionradish posted:

Is ENV representative of a full path (C:\my_python_files\), a subdirectory of c:\Python27, or some alias that refers to a full path?

It can be absolute or relative path to wherever you want create the environment. If you're in C:\my_python_files, virtualenv test will create C:\my_python_files\test and put all the stuff inside. Generally you don't make it a subdirectory of Python installation.

onionradish posted:

Also, when I'm installing modules/packages, do I install to a project's particular virtualenv, to the native Python directory or both? For example, if I want a module like numpy or PIL to be available to every script, I'm assuming that I install it to native Python directory. For my actual project, I'm assuming I'll also need to install it to its particular virtualenv so that version is the one that will run.

Usually you want to install to just the virtualenv (after you activate it, pip install X will do the right thing). If you want something to be available from global site-packages, you need to create virtualenv with --site-packages argument. Then you don't have to install it again to the virtualenv.

onionradish
Jul 6, 2006

That's spicy.
Awesome! Thanks, everybody. Glad I asked!

fritz
Jul 26, 2003

The Gripper posted:

I have no idea what the fashionable thing would be to do, but it can be done with decorators if the wrapper code is always going to be the same, through something like:

Dang, I hadn't even thought about decorators. Hmm. (Thanks!)

Daynab
Aug 5, 2008

So, working with wxPython to make an interface... are you supposed to create the whole UI completely within the __init__? That's how the tutorials show it http://wiki.wxpython.org/Getting%20Started but it seems a bit... weird to do that.

Masa
Jun 20, 2003
Generic Newbie

Daynab posted:

So, working with wxPython to make an interface... are you supposed to create the whole UI completely within the __init__? That's how the tutorials show it http://wiki.wxpython.org/Getting%20Started but it seems a bit... weird to do that.

I can't speak for wx specifically, but in PyQt you have a class for each window (So one for the main window, another one for say, an options dialog, and then another for something else, etc.) and you do indeed put everything you want for that window in __init__ for that class.

duck monster
Dec 15, 2004

Daynab posted:

So, working with wxPython to make an interface... are you supposed to create the whole UI completely within the __init__? That's how the tutorials show it http://wiki.wxpython.org/Getting%20Started but it seems a bit... weird to do that.

Use this thing: http://kivy.org/

It makes incredibly cool UIs.

onionradish
Jul 6, 2006

That's spicy.
Following up on virtualenv, I'm having failures when trying to install packages, and I'm not sure what I'm doing wrong.

I'm on Windows, and using CMD as the shell. I'm able to create a project folder: "virtualenv test". After I do that, I'm navigating to the test directory (D:\test\project) and entering "scripts\activate". Then I'm trying "pip install lxml" as an example.

A bunch of stuff scrolls by and results in "failed with error code 1". I'm not sure what this means:
code:
Command D:\test\project\Scripts\python.exe -c "import setuptools;__file__='
c:\\windows\\temp\\pip-j7v8xg-build\\setup.py';exec(compile(open(__file__).read(
).replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\windows\temp\pi
p-q2edge-record\install-record.txt --single-version-externally-managed --install
-headers D:\test\project\include\site\python2.7 failed with error code 1 in
 c:\windows\temp\pip-j7v8xg-build
For what it's worth, I don't see "D:\test\project\include\site\*" as a folder; the "include" path only has a bunch of *.h files. What am I doing wrong?

Haystack
Jan 23, 2005





You have just run into the bane of windows python programmers everywhere: compiler issues. Lots of python libraries have have c extensions that pip tries to compile. Installing the visual studio 2008 redistributable will solve 90% of your issues.

Unfortunantely, lxml is in the 10% :shepicide:. The easiest way I know of getting it is to use the binaries from here to install lxml to your base python directory, and then copying the goddamn lxml folder to your virtualenv (as detailed on this stackoverflow page)

Adbot
ADBOT LOVES YOU

Jewel
May 2, 2009

Haystack posted:

You have just run into the bane of windows python programmers everywhere: compiler issues. Lots of python libraries have have c extensions that pip tries to compile. Installing the visual studio 2008 redistributable will solve 90% of your issues.

Unfortunantely, lxml is in the 10% :shepicide:. The easiest way I know of getting it is to use the binaries from here to install lxml to your base python directory, and then copying the goddamn lxml folder to your virtualenv (as detailed on this stackoverflow page)

Compiler and linking issues in general are absolutely rediculous. Just to link a library to a new project in Visual Studio I shouldn't have to go through hundreds of scattered options to find the right three or four, and then hope everything works on the exact version of windows I have with the exact dependencies I have and also work on other peoples'.

  • Locked thread