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
tbp
Mar 1, 2008

DU WIRST NIEMALS ALLEINE MARSCHIEREN

Thermopyle posted:

The universal answer to this is to write something to solve a problem you have.

After you pick up the basics in Think Python, start solving a problem or automating something you do all the time. Some ideas taken from my projects directory:


1) Write something to organize your download directory.
2) Something to fetch album art for your mp3's.
3) Scrape sports scores/bus schedule/whatever from some website.
4) Write a script to fetch emails and then take actions depending on contents of email. (Remote control your PC!)
5) Same as 4 but with IRC.
6) Fetch your Google Reader shared items and post them to IRC/forum/IM/etc
7) Fetch and graph some data from your modem/router web-admin
8) Organize your photos into folders by date.

Hahaha I suppose I'll continue just plodding through the books for now. Its funny because this seriously looks nigh impossible to me

Adbot
ADBOT LOVES YOU

Lurchington
Jan 2, 2003

Forums Dragoon

tripwire posted:

loving SOLD

Used it this afternoon, and was impressed with the source control integration and how it handle virtualenvs. Both of those were a bit wack on PyDev.

My boss and some other people had been loosely following the development, so I think my company might spring for some licenses :)

standardtoaster
May 22, 2009

tbp posted:

Hahaha I suppose I'll continue just plodding through the books for now. Its funny because this seriously looks nigh impossible to me

Yeah, I can't even fathom where to begin with anything like that. It's so abstract to me right now.

First Time Caller
Nov 1, 2004

standardtoaster posted:

Yeah, I can't even fathom where to begin with anything like that. It's so abstract to me right now.

The first project given is pretty easy. The key is to stop thinking about the code initially, and just start writing down a common sense procedure.

1) Write something to organize your download directory.

Think of some steps.

- Set the path to your download directory. (You'll want to use os.path, but to keep it simple you can hardcode it. i.e, "C:\MyDownloads\")

- Create a folder for every letter in the alphabet. i.e, for letter in range('a','z') You can use methods provided in the os module to do so.

- Loop through all of the files in the download folder and store their path/filename.extension as a string in a list.

- Sort the list

- Loop through the list and use provided methods in the os module to move each file into the correct folder, one at a time.

It's not so abstract anymore, you can probably write a script to do this now. When you have this working consider trying to do the same thing but instead of sorting the files into folders alphabetically, sort them into folders by media type.

Some psuedocode:
code:
media_types = {
     'documents':['txt', 'doc', 'docx'],
     'photos':['gif','jpg','jpeg','png'],
     'videos':['avi','mpg','mpeg','wmv']
}
for type in media_types.keys():
     makefolder(type) #create a folder for each media type

for filename in list_of_files:
     extension = filename[filename.find('.'):]
     if extension in media_types['documents']:
          move_to(documents_folder, path+filename)
     elif extension in media_types['photos']:
          move_to(photos_folder, path+filename)
     elif extension in media_types['videos']:
          move_to(videos_folder, path+filename)
     else:
          move_to(unknowns_folder, path+filename)
Then keep improving. That second forloop I made is pretty bad. You have to change extra code whenever you add a media type. You can try and write a for loop that searches through each key in the media_types dictionary, stores the key, loops through its extensions and if there's a match, calls a move_to function using the dictionary key as the destination folder name.

First Time Caller fucked around with this message at 18:20 on Oct 13, 2010

Stabby McDamage
Dec 11, 2005

Doctor Rope
What is the best way to modify a list in arbitrary, flexible ways? For example, to replace every element for which function f() returns true with three new elements returned by a function g(). E.g.
code:
list = [1,4,9,500]
def f(n): return n%2==0
def g(n): return (n,n+1,n+2)

fancy_construct(list,f,g)

# list is now [1, 4,5,6, 9, 500,501,502]
What about if the check depends on multiple elements, such as "insert a 5 between every contiguous 9 and 10"?

I'm sure I could find some method to do this based on insert() and pop() and such, but I bet there's some elegant way to do it. I thought of using a list comprehension, but I don't see how to expand one element to more than one.

EDIT: I found a big part of the answer: slice assignments. "a[1:1] = [2,3,4]" will insert 2,3,4 at offset 1, replacing the original element 1.

Stabby McDamage fucked around with this message at 18:53 on Oct 13, 2010

Lurchington
Jan 2, 2003

Forums Dragoon

Stabby McDamage posted:

What is the best way to modify a list in arbitrary, flexible ways? For example, to replace every element for which function f() returns true with three new elements returned by a function g(). E.g.
code:
list = [1,4,9,500]
def f(n): return n%2==0
def g(n): return (n,n+1,n+2)

fancy_construct(list,f,g)

# list is now [1, 4,5,6, 9, 500,501,502]
What about if the check depends on multiple elements, such as "insert a 5 between every contiguous 9 and 10"?

I'm sure I could find some method to do this based on insert() and pop() and such, but I bet there's some elegant way to do it. I thought of using a list comprehension, but I don't see how to expand one element to more than one.

sounds like a variation on reduce with map

Stabby McDamage
Dec 11, 2005

Doctor Rope

Lurchington posted:

sounds like a variation on reduce with map

I don't see how reduce could be related, since no part of this involves condensing a list down to one value. For map, I thought of that, but I don't see how that could be used here, because if the given function returns (2,3,4), then the tuple (2,3,4) gets included in the list, not the elements 2,3,4.

EDIT: This is my best effort at implementing the fancy_construct used above. I think it's pretty ugly and non-pythonic...surely there's a better way.
code:
def fancy_construct(a,f,g):
    i=0
    while i<len(a):
        print "Looking at i=%d a[i]=%s" % (i,a[i])
        if f(a[i]): 
            replacement = g(a[i])      # find replacement
            a[i:i+1] = replacement     # replace element with new values
            i += len(replacement) - 1  # adjust i explcitly so we don't scan our own replacement
        i+=1

Stabby McDamage fucked around with this message at 19:09 on Oct 13, 2010

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Stabby McDamage posted:

I don't see how reduce could be related, since no part of this involves condensing a list down to one value.

Sure it does. You are condensing a list down to one value, which happens to be a new list.

code:
list = [1,4,9,500]

def f(n): return n%2==0

def g(n): return [n, n+1, n+2]

def build(l, n):
  if f(n):
    return l + g(n)
  else:
    return l + [n]

newlist = reduce( build, list, [] )

tbp
Mar 1, 2008

DU WIRST NIEMALS ALLEINE MARSCHIEREN

standardtoaster posted:

Yeah, I can't even fathom where to begin with anything like that. It's so abstract to me right now.

Lets stick to it and learn this poo poo then. I'll continue with that PDF and soon we'll be taking on those tasks like its not problem

ToxicFrog
Apr 26, 2008


You can reduce it with list.__add__, I think, if you return [2,3,4] rather than (2,3,4); it's a reduction to a single value (which is itself a list)
code:
def f(i):
    return i % 2 == 0

def g(i):
    return [i, i**2, i**3]

data = [ 1, 2, 3, 4, 5, 6 ]

reduce(list.__add__, map(lambda x: f(x) and g(x) or [x], data))
# output: [1, 2,4,8, 3, 4,16,64, 5, 6,36,216]
This generates a lot of small garbage lists, though. Does python have FlatMap?

Edit: beaten like an AST-walking interpreter by ShoulderDaemon

Stabby McDamage
Dec 11, 2005

Doctor Rope

ShoulderDaemon posted:

Sure it does. You are condensing a list down to one value, which happens to be a new list.

code:
list = [1,4,9,500]

def f(n): return n%2==0

def g(n): return [n, n+1, n+2]

def build(l, n):
  if f(n):
    return l + g(n)
  else:
    return l + [n]

newlist = reduce( build, list, [] )

Ah, I would never have thought of that. Thanks.

Thermopyle
Jul 1, 2003

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

tbp posted:

Lets stick to it and learn this poo poo then. I'll continue with that PDF and soon we'll be taking on those tasks like its not problem

Please do, it's worth it!

Feel free to ask questions. Did the post by First Time Caller help give you an idea what you should be thinking about?

tbp
Mar 1, 2008

DU WIRST NIEMALS ALLEINE MARSCHIEREN

Thermopyle posted:

Please do, it's worth it!

Feel free to ask questions. Did the post by First Time Caller help give you an idea what you should be thinking about?

...sort of. I get the idea that I shouldn't be going into solving problems with 'what code should I write' instead of 'what steps should the code take'. But even the first step is lost on me. Set a path? Huh?

First Time Caller
Nov 1, 2004

tbp posted:

...sort of. I get the idea that I shouldn't be going into solving problems with 'what code should I write' instead of 'what steps should the code take'. But even the first step is lost on me. Set a path? Huh?

The general idea of a filepath in the context of an operating system is a fully qualified reference to the location of the file, including its name. e.g, where it is on your hard drive. I'm sure you've seen this before when traversing your filesystem looking for a particular file. "C:\Windows\SecretPornSpot\BucksBuckingBitches.wmv"

You'll need to be able to reference the location of your downloads directory several times in your program. The easiest way to do this instead of typing it out several times is to declare a variable that holds the value.

code:
path_to_download_dir = "C:\MyDownloads\"
In this you are creating a string literal and binding its value to the variable name path_to_download_dir. Now instead of typing out "C:\MyDownloads\" in 20 different spots in your program, you can just use path_to_download_dir. (If for some reason you ever move your downloads folder somewhere else, you'll only have to change 1 line of code instead of several.)

It's likely that this will all make more and more sense as you go along. If you want to learn in a more traditional environment, MIT has an online Python course and actually, so does Google. A lot of the things they do in the MIT course are kind of ...odd... for Python but it will still teach you how to program so you might want to do the Google course instead, if you so choose.

I personally think that neither of these are as good as Think Like A... so I would suggest using them as supplementary tools only.

First Time Caller fucked around with this message at 23:25 on Oct 13, 2010

Lurchington
Jan 2, 2003

Forums Dragoon

ShoulderDaemon posted:

Sure it does. You are condensing a list down to one value, which happens to be a new list.


what he said

edit: Bah

I'll use this to say that I'm 2 days into PyCharm and I'm really liking it. Loving the code completion that actually works for a lot of duck typing and the highlighting for changes in a revision controlled file.

Lurchington fucked around with this message at 00:15 on Oct 14, 2010

tripwire
Nov 19, 2004

        ghost flow

Lurchington posted:

what he said

edit: Bah

I'll use this to say that I'm 2 days into PyCharm and I'm really liking it. Loving the code completion that actually works for a lot of duck typing and the highlighting for changes in a revision controlled file.

The one thing I'm missing is code-fragment mode while debugging; you can do that with manually with pdb but for some reason its missing in the gui.

tbp
Mar 1, 2008

DU WIRST NIEMALS ALLEINE MARSCHIEREN

First Time Caller posted:

The general idea of a filepath in the context of an operating system is a fully qualified reference to the location of the file, including its name. e.g, where it is on your hard drive. I'm sure you've seen this before when traversing your filesystem looking for a particular file. "C:\Windows\SecretPornSpot\BucksBuckingBitches.wmv"

You'll need to be able to reference the location of your downloads directory several times in your program. The easiest way to do this instead of typing it out several times is to declare a variable that holds the value.

code:
path_to_download_dir = "C:\MyDownloads\"
In this you are creating a string literal and binding its value to the variable name path_to_download_dir. Now instead of typing out "C:\MyDownloads\" in 20 different spots in your program, you can just use path_to_download_dir. (If for some reason you ever move your downloads folder somewhere else, you'll only have to change 1 line of code instead of several.)

It's likely that this will all make more and more sense as you go along. If you want to learn in a more traditional environment, MIT has an online Python course and actually, so does Google. A lot of the things they do in the MIT course are kind of ...odd... for Python but it will still teach you how to program so you might want to do the Google course instead, if you so choose.

I personally think that neither of these are as good as Think Like A... so I would suggest using them as supplementary tools only.

So by using that string the shell will know the go to the documents folder? Yeah that does make a bit more sense now.

First Time Caller
Nov 1, 2004

tbp posted:

So by using that string the shell will know the go to the documents folder? Yeah that does make a bit more sense now.

Not necessarily but I think you're getting it. That statement alone doesn't do anything special except bind the value of the string to a variable name. The Python interpreter doesn't know what it does or what it's for, but you do.

This is where the Python docs would come in handy. The os module provides many methods for interacting with your computers filesystem.

http://docs.python.org/library/os.html#os.mkdir

You'll notice the method header looks like this: os.mkdir(path[, mode]). It's read as "the module os provides a method mkdir that takes one required parameter, path, and one optional parameter, mode". You can call this method like:
code:
import os
os.mkdir("C:\MyDownloads\Media\")
# or
the_media_folder = "C:\MyDownloads\Media\"
os.mkdir(the_media_folder)

First Time Caller fucked around with this message at 02:24 on Oct 14, 2010

tbp
Mar 1, 2008

DU WIRST NIEMALS ALLEINE MARSCHIEREN
Thank you a lot First Time Caller I hope you expect to be answering many questions in the coming pages

I think the first project I may outline for myself is to do something along the lines of that - but sort information from the game Football Manager 2010. Its a really stat-heavy simulation game, basically a glorified spreadsheet, so writing a program would be both useful practice and useful in-game.

First Time Caller
Nov 1, 2004

tbp posted:

Thank you a lot First Time Caller I hope you expect to be answering many questions in the coming pages

I think the first project I may outline for myself is to do something along the lines of that - but sort information from the game Football Manager 2010. Its a really stat-heavy simulation game, basically a glorified spreadsheet, so writing a program would be both useful practice and useful in-game.

I'm learning the language as well and it helps me a lot by helping others so I'd be more than happy to answer as many questions as I can. Some more experienced python programmers will probably need to jump in at times and correct me but that's all part of the learning process.

That's a great idea for a first project. Can you post up one of the files that you'd be parsing for FM2010?

Karate Bastard
Jul 31, 2007

Soiled Meat
I have an annoying problem. I feel I can't be the first to come across it, but neither google nor docs have been of any help to me so far.

I have a largish 2d numpy char array (exact size varies, but typically ~17000x700 elements) and from this I want to compute a bool array of the same size which is True everywhere except in positions that contain one of a few chars (almost always 2-3 and always nonconsecutive), where I want it to be False. I also need this to be Fast!

This snippet solves my problem but is too slow for my liking:
code:
falsechars = '.- ' # or something...
ch_arr = numpy.frombuffer(the17000x700str, dtype='|S1') # already given.
b_arr = ch_arr != falsechars[0]
for ch in falsechars[1:]:
    b_arr &= ch_arr != ch
since it traverses the char array 3 times and makes 2 bitwise array ands. OK, it's decent for python, but it ain't Fast! enough for my purposes.

This one is even worse:
code:
condition = numpy.vectorize(lambda ch: ch in falsechars)
b_arr = condition(ch_arr)
since it makes a gazillion calls to a python func instead of making simple C == checks.

Making a C extension for this would probably be stupidly easy, and would probably also be the fastest solution in the end, but my gut feeling tells me there's probably something in the numpy library that I missed. I can't be the first one who wants to do this stuff.

tripwire
Nov 19, 2004

        ghost flow
Maybe its not a big deal with three characters in your list of excluded letters, but you should probably turn that string into a set if you want faster membership testing. How you'd do that in numpy without calling a python function on each cell is beyond me though.

tripwire
Nov 19, 2004

        ghost flow
code:
>>> import cProfile
>>> import numpy
>>> #lets make an array of random characters
... nchr = numpy.vectorize(chr, otypes=[str])
>>> my_array = nchr(numpy.random.random_integers(0,255,(17000,700))).astype('|S1')
>>>
>>> def exclude1(in_array,excluded_letters):
...     out_array = in_array != excluded_letters[0]
...     for letter in excluded_letters[1:]:
...         out_array &= in_array != letter
...     return out_array
...
>>> def exclude2(in_array,excluded_letters):
...     excluded_letters = set(excluded_letters)
...     foo = numpy.vectorize(
...         lambda x: x not in excluded_letters,
...         otypes=[bool])
...     out_array = foo(in_array)
...     return out_array
...
>>> #how do they compare with only 3 excluded characters?
... cProfile.run("exclude1(my_array,'.- ')")
         3 function calls in 1.451 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    1.450    1.450    1.450    1.450 <stdin>:1(exclude1)
        1    0.001    0.001    1.451    1.451 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


>>> cProfile.run("exclude2(my_array,'.- ')")
         11900019 function calls in 9.933 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.025    0.025    9.932    9.932 <stdin>:1(exclude2)
 11900001    3.953    0.000    3.953    0.000 <stdin>:4(<lambda>)
        1    0.001    0.001    9.933    9.933 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 function_base.py:1669(_get_nargs)
        1    0.000    0.000    0.000    0.000 function_base.py:1759(__init__)
        1    5.954    5.954    9.907    9.907 function_base.py:1785(__call__)
        1    0.000    0.000    0.000    0.000 function_base.py:36(iterable)
        1    0.000    0.000    0.000    0.000 numeric.py:201(asarray)
        1    0.000    0.000    0.000    0.000 {callable}
        1    0.000    0.000    0.000    0.000 {hasattr}
        3    0.000    0.000    0.000    0.000 {isinstance}
        1    0.000    0.000    0.000    0.000 {iter}
        1    0.000    0.000    0.000    0.000 {len}
        1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method 'join' of 'str' objects}
        1    0.000    0.000    0.000    0.000 {numpy.core.umath.frompyfunc}


>>>
>>> #ok so the first one is obviously faster. but how about 26 characters?
...
>>> from string import uppercase
>>> cProfile.run("exclude1(my_array,uppercase)")
         3 function calls in 12.774 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1   12.773   12.773   12.773   12.773 <stdin>:1(exclude1)
        1    0.001    0.001   12.774   12.774 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


>>> cProfile.run("exclude2(my_array,uppercase)")
         11900019 function calls in 9.909 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.024    0.024    9.908    9.908 <stdin>:1(exclude2)
 11900001    3.894    0.000    3.894    0.000 <stdin>:4(<lambda>)
        1    0.001    0.001    9.909    9.909 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 function_base.py:1669(_get_nargs)
        1    0.000    0.000    0.000    0.000 function_base.py:1759(__init__)
        1    5.991    5.991    9.884    9.884 function_base.py:1785(__call__)
        1    0.000    0.000    0.000    0.000 function_base.py:36(iterable)
        1    0.000    0.000    0.000    0.000 numeric.py:201(asarray)
        1    0.000    0.000    0.000    0.000 {callable}
        1    0.000    0.000    0.000    0.000 {hasattr}
        3    0.000    0.000    0.000    0.000 {isinstance}
        1    0.000    0.000    0.000    0.000 {iter}
        1    0.000    0.000    0.000    0.000 {len}
        1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method 'join' of 'str' objects}
        1    0.000    0.000    0.000    0.000 {numpy.core.umath.frompyfunc}

Karate Bastard
Jul 31, 2007

Soiled Meat
Turns out I wasn't first at all. I was so not first it's already in punctuation.

code:
ch_arr = ... # huge char array, given.
rules = numpy.ones(256, bool)
rules[[ord(s) for s in excluded]] = False

byte_arr = numpy.frombuffer(ch_arr.data, numpy.uint8) # no copy!
bool_arr = rules[byte_arr] # "fancy indexing" := numpy.take()
I guess this boils down to some syntactic sugar around a pointer cast. It's darn snappy. I'm discovering new stuff you can do with numpy every day now. It's really fast and powerful when used right.

Opinion Haver
Apr 9, 2007

code:
def alpha_beta_search(board, depth,
                      eval_fn,
                      get_next_moves_fn=get_all_next_moves,
		      is_terminal_fn=is_terminal):
    def evaluate(board, depth, alpha, beta):
        # if it's a terminal node, then perform static evaluation
        if is_terminal_fn(depth, board): return eval_fn(board)
        for move, new_board in get_next_moves_fn(board):
            alpha = max(alpha, -evaluate(new_board, depth - 1, -beta, -alpha))
            if beta <= alpha: break
        return alpha
    action, new_board = max(get_next_moves_fn(board), key=lambda (a, n): -evaluate(n, depth, -INFINITY, +INFINITY))
    return action
Is this a correct, speed-optimized version of alpha-beta search? It looks correct, but it's running slower than the test suite for my class allows.

e: oh hurf that 'depth' in the max call needs to be depth-1, it still seems slow though.

Opinion Haver fucked around with this message at 01:34 on Oct 17, 2010

tbp
Mar 1, 2008

DU WIRST NIEMALS ALLEINE MARSCHIEREN

First Time Caller posted:

I'm learning the language as well and it helps me a lot by helping others so I'd be more than happy to answer as many questions as I can. Some more experienced python programmers will probably need to jump in at times and correct me but that's all part of the learning process.

That's a great idea for a first project. Can you post up one of the files that you'd be parsing for FM2010?

Yeah I'll do this in a bit. Basically I'm looking to sort by stats to have a go-to reference for what players to purchase etc.

sink
Sep 10, 2005

gerby gerb gerb in my mouf
Do you guys have any strategies for refactoring and maintaining large systems in Python?

I began working on Python over a year ago after working as a Java programmer for a number of years. I loved Python from the start.

However now that the programs that I have been working on (mostly big Django apps) have grown in size and complexity, I am starting to miss some of the rigidity that came with Java.

Static typing, forcing the catching or passing of exceptions, a compile step were all helpful. Forcing everything into the object paradigm, while obnoxious, was also useful sometimes.

Does anyone have any advice on disciplines that I should be adhering to when writing Python code to improve future maintainability? Does anyone have any advice or suggestions for tools for refactoring or checking my code?

king_kilr
May 25, 2007
Tests!

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

sink posted:

Do you guys have any strategies for refactoring and maintaining large systems in Python?

I began working on Python over a year ago after working as a Java programmer for a number of years. I loved Python from the start.

However now that the programs that I have been working on (mostly big Django apps) have grown in size and complexity, I am starting to miss some of the rigidity that came with Java.

Static typing, forcing the catching or passing of exceptions, a compile step were all helpful. Forcing everything into the object paradigm, while obnoxious, was also useful sometimes.

Does anyone have any advice on disciplines that I should be adhering to when writing Python code to improve future maintainability? Does anyone have any advice or suggestions for tools for refactoring or checking my code?

You might want to take a look at pylint?

libberlibber
Apr 14, 2004

by Y Kant Ozma Post
Just found this subforum and thread. Pycharm is awesome, thanks.

Lurchington
Jan 2, 2003

Forums Dragoon

libberlibber posted:

Just found this subforum and thread. Pycharm is awesome, thanks.

PyCharm cool thing of the day: VCS (we use mercurial) history of the active file your editing.

'who messed up my poo poo?'
*check the history*
'hmm, sammy jones in changeset 63, that bastard'

UberJumper
May 20, 2007
woop

Lurchington posted:

PyCharm cool thing of the day: VCS (we use mercurial) history of the active file your editing.

'who messed up my poo poo?'
*check the history*
'hmm, sammy jones in changeset 63, that bastard'

Yeah their VCS stuff is pretty drat nifty. The only major issue i have left with pycharm is:

- Debugger seems to have problems with hitting breakpoints of modules i don't import directly:

code:
<test.py>
import hat.cats

<hat/cats.py>
import hat.common
No break points get hit in hat.common. I know it gets executed. But i want it to break :(

- It can sometimes bug the living poo poo out of me with its inspections, most of the time its fantastic. "Yes PyCharm i know that line isn't right because i only started typing the first loving letter of it"

But poo poo this is easily the best IDE i have used for python.

Also i have a question for you guys i am trying to wrap our existing hellish ftplib wrapper library (:suicide:) into something more Pythonic. Pythonic is the new buzzword at work, since we are porting/rewriting our existing pre 2.0 era python libraries into the much more modern 2.5.4 era (blame arcgis) everything should be Pythonic (<insert something>-like objects, list comphrensions, packages, etc). Its a good idea but its getting to the point where its difficult to make certain things "Pythonic".

Anyways the library does stuff like resuming transfers, partial downloading, and properly expanding, parsing some internal binary formats and lots of other stuff. All in all the library is ~8k lines of code (excluding comments) crammed into half a dozen functions in a single .py file:

I have already done everything else, i have the core of it, its just when it comes to resumable downloads(this gets used alot, ~2GB files we want to resume, and it happens alot). To resume a download, we append a .lok extension to the filename, and only when the transfer is completed do we rename the file to the correct extension. This way if the transfer fails, when we reconnect we see .lok file and just continue the transfer from where it left off.

However i have no idea how to work something like this into a file like object :

code:
### Downloading a binary...
# Open a file on the ftp server in read-binary mode
f = fooftp.open('/hello/pr0n/folder/wtfcaterpie.jpg', 'rb', block_size=8192)
local_file = ?!?!?!
# wander through each of the blocks
for block in f:
   local_file.write(block)

# close
local_file.close()

# send quit to ftp
f.close()
I did doing the following

code:
remote_file = fooftp.open('/hello/pr0n/folder/wtfcaterpie.jpg', 'rb', block_size=8192)
local_file = remote_file.open_local(r'C:\workspace\dog.jpg', 'wb')
This however is considered unpythonic by our code review and was rejected :suicide: Does anyone have any ideas of how the gently caress i am supposed to make this pythonic? Uploading a file is fine because i can wrap all of the resuming stuff. I feel pretty stupid atm =(

:commissar: Pythonic :eng101:

good jovi
Dec 11, 2000

UberJumper posted:

This however is considered unpythonic by our code review and was rejected :suicide: Does anyone have any ideas of how the gently caress i am supposed to make this pythonic? Uploading a file is fine because i can wrap all of the resuming stuff. I feel pretty stupid atm =(

:commissar: Pythonic :eng101:

Well. That's more than slightly insane. They can just reject something as "un-pythonic" without providing any alternatives or suggestions? That's now how code review works.

I actually prefer your original code. Is it considered un-pythonic in any specific way, or are you just expected to be a mind reader?

notMordecai
Mar 4, 2007

Gay Boy Suicide Pact?
Sucking Dick For Satan??

I have a weird rear end question to ask for those familiar with using IDLE on Linux.

I had just starting programming in python but I currently only have access to an Ubuntu machine. I ran a simple program but when it didn't run correctly, I closed the window made the corrections and ran the module again only to see an error saying that the "Python shell is already executing a command".

The only way to fix the problem was to close IDLE completely and reopen it. I never encountered this problem using IDLE in windows so I have no idea what might be causing it.

Not only am I knew to the language but I am also new to the OS. Any help for a complete newbie? :blush:

First Time Caller
Nov 1, 2004

Doesn't answer your question directly but I have an alternative solution.

Don't use IDLE.

You'll be able to practice your terminal-fu at the same time!

niff
Jul 4, 2010
I didn't think this was worth it's own thread - I'm a beginner programmer, aspiring to be a game developer. Currently doing a university certificate to improve my rusty maths skills, which comes attached with a paper in C#. I know the basics and applications of loops, if/else, classes, arrays and a few other things in C#, and it seems like python would be a really good language to tinker about with right now - especially with making small simple games and applications, and how it can be used in web development later on which also interests me greatly. Its syntax is simple and easy to understand, and I figure it would be a friendly language to learn more programming concepts in order to become more efficient and learn.

Two questions spring to mind. I am used to working in visual studio, are there any IDEs for python that aren't too resource intensive for my (soon to be upgraded) 4 year old laptop? Or is it recommended to use notepad++ and a separate compiler so I learn without being auto-corrected?

Question 2, I have installed python 2.7 in order to use pyglet, but the pyglet installation doesn't seem to want to work and says it requires python 2.4 or higher. I heard it was very easy to make forms and do game-related things. Is this a common problem? Running 32-bit Vista (sigh).

Thanks in advance.

Thermopyle
Jul 1, 2003

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

I'm writing a script to sit between a program's web API (it's XBMC to be specific) and clients that access that API.

For example, you can hit XBMC with...

code:
http://host/xbmcCmds/xbmcHttp?command=setvolume&parameter=5
What I'd like is to direct clients to hit my script with that request and then my script will hit multiple XBMC installations with the corresponding request.

My question is this...what's a lightweight framework/web server that would be good for implementing this? The only thing I'm somewhat familiar with is Django, which just seems too massive for such a thing, besides the fact that the docs constantly say not to use the built-in webserver for anything in production.

I can't imagine any realistic scenario where there would be more than a few clients and a few XBMC installations, so I definitely don't need anything that is meant to handle massive amounts of requests.

niff posted:

I am used to working in visual studio, are there any IDEs for python that aren't too resource intensive for my (soon to be upgraded) 4 year old laptop? Or is it recommended to use notepad++ and a separate compiler so I learn without being auto-corrected?

Give PyCharm a try, people seem to be diggin' it.

First Time Caller
Nov 1, 2004

niff posted:

Two questions spring to mind. I am used to working in visual studio, are there any IDEs for python that aren't too resource intensive for my (soon to be upgraded) 4 year old laptop?

Question 2, I have installed python 2.7 in order to use pyglet, but the pyglet installation doesn't seem to want to work and says it requires python 2.4 or higher. I heard it was very easy to make forms and do game-related things. Is this a common problem? Running 32-bit Vista (sigh).

PyCharm as previously suggested or Eclipse+PyDev. I prefer Eclipse, but that's just me.

As for pyglet:

pyglet installer posted:

pyglet requires Windows XP (Windows Vista is not supported at this time).

Thermopyle posted:

My question is this...what's a lightweight framework/web server that would be good for implementing this?

This sounds like a good project for Web.Py

bitprophet
Jul 22, 2004
Taco Defender

First Time Caller posted:

This sounds like a good project for Web.Py

And for the love of God, don't confuse it with the newer project that thought it would be a great idea to name itself "web2py". Totally different (and crap).

Also check out Bottle, which is in the same vein as web.py, to see which one has a more pleasing-to-you API.

Adbot
ADBOT LOVES YOU

First Time Caller
Nov 1, 2004

I had not seen Bottle before but looking through the source code it looks great. I might actually have to learn the poo poo out of it as part of my python learning experience. 2100 lines of code is terribly digestible, even by someone as new as me.

  • Locked thread