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
Reformed Pissboy
Nov 6, 2003

Oddhair posted:

I keep getting:
code:
C:\Learpython>python hash_processor.py
Traceback (most recent call last):
  File "hash_processor.py", line 18, in <module>
    date = split_line[2]
IndexError: list index out of range
This is trying it with 'somefile.rtf' pointed to the file in question in the root of my C drive. I tried with both python 2.7 and 3.2, same results. I really appreciate the help, I've been throwing up since last night, and it's slowing my learning down quite a bit.

This indicates that it found a line somewhere that begins with "File Created", but doesn't have any other spaces in it. I can only guess that this means somewhere in your file there's a line that just says "File Created" or "File Created\par" or something but doesn't have a date. You can get more information about any of the variables that are being used by printing them out; this will also help show if things are working at all or if my crazy assumptions based on your sample file are off-base.

code:
if line.startswith('File Created'):
   split_line = line.split()

   # we're expecting something like:
   # split_line = ['File',  'Created', '09/02/12', '11:13:42PM']
   # we want:      0        1          *2*         3
   # just in case, though, let's see what we actually got:
   print split_line

   # the next line will fail if our line didn't have at least 2 separate spaces in it,
   # so let's test and see what we have
   if len(split_line) < 3:
      # this is a problem, we were expecting at least three things! ('File', 'Created', and some kind of date)
      print "This line doesn't follow the format we expected:", line
      # give up and quit in case this throws the rest of the loop out of whack
      break
   else:
      # yaaaaaay
      date = split_line[2]
Now that I think about it, it'll probably be a good idea to change those calls to replace \par and \tab with a space (' ') instead of an empty string ('') just in case that's throwing off split() somehow :I

Adbot
ADBOT LOVES YOU

Oddhair
Mar 21, 2004

Yep, there are apparently some files without dates, but I'm reasonably sure they all have hashes. I'm looking at this now, but I'm too green to modify it to allow for either the date or the hash or both being empty.

No biggie, I've gotten a PM from someone interested in doing it, thanks everyone for all your help.

Oddhair fucked around with this message at 14:58 on May 10, 2011

mst4k
Apr 18, 2003

budlitemolaram

Hmm, I'm using Python to generate a 65x75 array file in a dumb game I'm working on in my spare time. I'm still pretty new to Python and desktop programming in general so I may be doing something completely wrong here.

I'm using code like this to save a file filled with Tile objects:

code:
map = [[ galaxy.Tile(True, 219, random.choice(feature_color), libtcod.black)
            for y in range(MAP_HEIGHT) ]
                for x in range(MAP_WIDTH) ]

randfile = random.randrange(10000,99999,1)
mapf = str(os.getcwd()) + "\\tmp\\"  + str(title[:3]).upper() + str(randfile) + '.pln'
file = open(str(mapf), "wb")
pickle.dump(map, file, -1)
file.close()
And here is the tile object

code:
class Tile:
    def __init__(self, terrain, char = None, color = None,
    bkcolor = None, blocked = None, blocked_sight = None,
    explored = None):
        self.terrain = terrain
        self.obj = []
        self.char = char
        self.color = color
        self.background = bkcolor
        self.blocked = blocked
        self.blocked_sight = blocked_sight
        self.explored = explored
This works okay - I can read the file back just fine and everything works - the only problem being that it's slow as hell and the file size is about 260kb (using shelve its like 900kb). This is to save a planet and I want to have lots of planets so this could make total save files really huge. I know I could use cPickle to speed it up but is there something I'm missing about the file size?

Dren
Jan 5, 2001

Pillbug

karma_coma posted:

Hmm, I'm using Python to generate a 65x75 array file in a dumb game I'm working on in my spare time. I'm still pretty new to Python and desktop programming in general so I may be doing something completely wrong here.

I'm using code like this to save a file filled with Tile objects:

code:
map = [[ galaxy.Tile(True, 219, random.choice(feature_color), libtcod.black)
            for y in range(MAP_HEIGHT) ]
                for x in range(MAP_WIDTH) ]

randfile = random.randrange(10000,99999,1)
mapf = str(os.getcwd()) + "\\tmp\\"  + str(title[:3]).upper() + str(randfile) + '.pln'
file = open(str(mapf), "wb")
pickle.dump(map, file, -1)
file.close()
And here is the tile object

code:
class Tile:
    def __init__(self, terrain, char = None, color = None,
    bkcolor = None, blocked = None, blocked_sight = None,
    explored = None):
        self.terrain = terrain
        self.obj = []
        self.char = char
        self.color = color
        self.background = bkcolor
        self.blocked = blocked
        self.blocked_sight = blocked_sight
        self.explored = explored
This works okay - I can read the file back just fine and everything works - the only problem being that it's slow as hell and the file size is about 260kb (using shelve its like 900kb). This is to save a planet and I want to have lots of planets so this could make total save files really huge. I know I could use cPickle to speed it up but is there something I'm missing about the file size?

Avoid calling your variable 'map', map is a keyword in python.

To put your filename together use os.path.join().

You incur overhead associated with the objects when you pickle your data which makes your files larger than they need be. Consider writing your planet data to disc in a binary format rather than by using pickle. The struct module will help you read and write binary.

mst4k
Apr 18, 2003

budlitemolaram

Dren posted:

Avoid calling your variable 'map', map is a keyword in python.

To put your filename together use os.path.join().

You incur overhead associated with the objects when you pickle your data which makes your files larger than they need be. Consider writing your planet data to disc in a binary format rather than by using pickle. The struct module will help you read and write binary.

Ahh thanks for all the info. That was exactly what I was looking for. Thanks!

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

So I've written a life like cellular automaton thing using pygame and numpy. Everything works fine except my function for updating the grid runs like rear end. Does anyone have any suggestions to get it to run faster? I've already cut the run time of this function by like 40% over my first version, but it was so bad to begin with that it's still holding everything else up.

code:
def Update(grid):
    newgrid = zeros(grid.shape, dtype=int)
    for r in range(1,grid.shape[0]-3):
        for c in range(1,grid.shape[1]-3):
            neighborhood = ((r-1,c-1),(r-1,c),(r-1,c+1),(r,c-1),(r,c+1),(r+1,c-1),(r+1,c),(r+1,c+1))
            value = 0
            for i in neighborhood:
                if grid[i[0]][i[1]]:
                    value += 1
            if grid[r][c]:
                if value in S:
                    newgrid[r][c] = 1
            else:
                if value in B:
                    newgrid[r][c] = 1
    return newgrid
e: practically all of the overhead is in

code:
            for i in neighborhood:
                if grid[i[0]][i[1]]:
                    value += 1
I already tried rewriting that with map but it actually slowed everything down slightly.

fart simpson fucked around with this message at 19:28 on May 10, 2011

Dren
Jan 5, 2001

Pillbug
You could store two grids: the grid you have now and a grid of neighbor values.

That value you compute could be generated as you update the grid for each iteration of your game. This will save you from doing the check on all 8 neighbors and reduce your runtime from R * C * 8 to R * C. (Where R and C are the sizes of the ranges)

e.g.
code:
def Update(grid, neighbors):
    newgrid = zeros(grid.shape, dtype=int)
    newneighbors = zeros(neighbors.shape, dtype=int)
    for r in xrange(1, grid.shape[0] - 3):
        for c in xrange(1, grid.shape[1] - 3):
            if grid[r][c] and neighbors[r][c] in S:
                newgrid[r][c] = 1
                newneighbors[r-1][c-1] += 1
                newneighbors[r-1][c]   += 1
                # ... etc
            elif neighbors[r][c] in B:
                newgrid[r][c] = 1
                newneighbors[r-1][c-1] += 1
                newneighbors[r-1][c]   += 1
                # ... etc
    
    return (newgrid, newneighbors)
I have taken the liberty of changing range to xrange. xrange is a generator. It is preferable to range because range creates a list which takes up memory.

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Thanks, that made it run almost 7 times faster in practice.

king_kilr
May 25, 2007
Use PyPy.

Dren
Jan 5, 2001

Pillbug

MeramJert posted:

Thanks, that made it run almost 7 times faster in practice.

Sweet! Just to be computer sciency about it, the tradeoff there is memory/runtime and you can often make that trade in order to optimize one or the other.

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

king_kilr posted:

Use PyPy.

Can you get numpy to work with pypy?

e: yeah, this isn't really applicable to me at all since I'm basing everything on pygame and numpy, both of which are incompatible with pypy.

fart simpson fucked around with this message at 19:20 on May 11, 2011

king_kilr
May 25, 2007

MeramJert posted:

Can you get numpy to work with pypy?

e: yeah, this isn't really applicable to me at all since I'm basing everything on pygame and numpy, both of which are incompatible with pypy.

Totally working on it: http://morepypy.blogspot.com/2011/05/numpy-in-pypy-status-and-roadmap.html http://morepypy.blogspot.com/2011/05/numpy-follow-up.html . Someone is playing with getting enough of cpyext working to use pygame as well. We totally understand people want NumPy, we want NumPy too.

Ninja edit: if people have questions about pypy feel free to ask me

Hughlander
May 11, 2005

king_kilr posted:

Totally working on it: http://morepypy.blogspot.com/2011/05/numpy-in-pypy-status-and-roadmap.html http://morepypy.blogspot.com/2011/05/numpy-follow-up.html . Someone is playing with getting enough of cpyext working to use pygame as well. We totally understand people want NumPy, we want NumPy too.

Ninja edit: if people have questions about pypy feel free to ask me

Is PyPy in/going to be in pydev to replace psyco? (Particularly for eclipse under windows.)

king_kilr
May 25, 2007

Hughlander posted:

Is PyPy in/going to be in pydev to replace psyco? (Particularly for eclipse under windows.)

Does PyDev ship psyco or something? At this point psyco is no longer actively maintained, it's primary author, Armin Rigo has been working on PyPy from the very beginning.

Hughlander
May 11, 2005

king_kilr posted:

Does PyDev ship psyco or something? At this point psyco is no longer actively maintained, it's primary author, Armin Rigo has been working on PyPy from the very beginning.

It complains when psyco isn't found about not being able to use it, when I looked into it/asked about it a year or so back the answer was 'we're waiting for pypy to be stable/release and won't be supporting psyco anymore' (Particularly on 2.7 obviously.)

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

I have a question that's more a general algorithm question than a Python question. So if I had a very large grid, and randomly moving particles were spread out all across it, what would be a good way to select 1 particle and tell which other particles are within a certain distance of the selected one? The only way I can think of involves checking the position of every particle in the whole system to see if they are in range, but there has to be a better way to do it.

brosmike
Jun 26, 2009

MeramJert posted:

I have a question that's more a general algorithm question than a Python question. So if I had a very large grid, and randomly moving particles were spread out all across it, what would be a good way to select 1 particle and tell which other particles are within a certain distance of the selected one? The only way I can think of involves checking the position of every particle in the whole system to see if they are in range, but there has to be a better way to do it.

One idea might be to keep track of a mapping from chunks of your grid to individual particles. So say your grid went from (0,0) to (100,100) - you might keep a mapping from 10x10 squares to a set of the particles contained within the squares. Keeping track of this would only require a constant-time addition to your physics tick process, but if your particles are generally not clustered together would significantly reduce the amount of particles you need to check (just check those from the 10x10 squares near the target).

You can be cleverer than this by allowing for variable-sized partitions (ie, not all of your squares are 10x10), using smaller ones where particles are more densely gathered. This is much harder, especially if the "dense areas" move over time, but might be more effective if you have particles clustered together all the time.

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Hmm, I was sort of tossing a similar idea around in my head. It reminds me of loading zones in an open world RPG. This seems like it must be some sort of solved problem already, though?

VirtuaSpy
Jul 26, 2002

OMG WTF FIREEEE!!

MeramJert posted:

I have a question that's more a general algorithm question than a Python question. So if I had a very large grid, and randomly moving particles were spread out all across it, what would be a good way to select 1 particle and tell which other particles are within a certain distance of the selected one? The only way I can think of involves checking the position of every particle in the whole system to see if they are in range, but there has to be a better way to do it.

There are various way to do this using clustering. I used a method called k-means clustering in a scientific computing course using Euclidean distance for clustering star data. So you could Google k-means and see how it works for you. It worked pretty good for me for a data set on the order of 100,000 stars in a large 3d space and clustered the data in 3 to 10 groups in about a minute using Python and numpy.

VirtuaSpy fucked around with this message at 18:25 on May 15, 2011

Captain Capacitor
Jan 21, 2008

The code you say?

MeramJert posted:

I have a question that's more a general algorithm question than a Python question. So if I had a very large grid, and randomly moving particles were spread out all across it, what would be a good way to select 1 particle and tell which other particles are within a certain distance of the selected one? The only way I can think of involves checking the position of every particle in the whole system to see if they are in range, but there has to be a better way to do it.

You might benefit from looking into K-D trees.

crazyfish
Sep 19, 2002

Possibly dumb question: How do I correctly do a format string that also contains a literal %? Example:

code:
# The second % in the string literal should not be interpreted as part of the format call
str = 'abc %s def %' % butts 
I've tried escaping the % but in all cases I still get a 'too few arguments for format string' error.

A A 2 3 5 8 K
Nov 24, 2003
Illiteracy... what does that word even mean?

crazyfish posted:

Possibly dumb question: How do I correctly do a format string that also contains a literal %? Example:

I've tried escaping the % but in all cases I still get a 'too few arguments for format string' error.

%%

crazyfish
Sep 19, 2002

A A 2 3 5 8 K posted:

%%

Thanks!

Hanpan
Dec 5, 2004

I'm using urllib2 to monitor a web socket I have the following class which handles connecting to the site and receiving a response:

code:
class Stream(Thread):

    def __init__(self, connectCallback=None, **kwargs):
        self.connectCallback = connectCallback
        Thread.__init__(self)

    def run(self):
        self.running = True

        while self.running:
            try:
                req = urllib2.Request(url, headers=self.headers)
                req.add_data(self.body)
                print "Stream connecting %s" % self
                self.connectCallback(self)
                f = urllib2.urlopen(req)
                print "Stream connected %s" % self
                while 1:
                    if self.running:
                        print "data receiving"
                    else:
                        f.close()
                        break
                retry_time = self.retry_time
            except (urllib2.URLError, urllib2.HTTPError), e:
                print 'Exception: %s, retry in %f' % (e, retry_time)
                time.sleep(retry_time)
                retry_time *= 4.0
                if retry_time > self.max_retry:
                    retry_time = self.max_retry
            except Exception as err:
                print 'Exception: %s' % err

        print "Stream closed %s" % self

    def disconnect(self):
        print 'Disconnecting stream %s ' % self
        if self.running is False:
            return
        self.running = False
        self.join()
I want to be able to run more than one instance of my Stream class asynchronously. Here is my main class which instantiates the Streams:

code:
class Consumer(object):

    def __init__(self):
        try:
            self.producer = self._open_stream()
            while 1:
                sleep(100)
        except KeyboardInterrupt:
            self.producer.disconnect()
            sys.exit()

    def onStreamConnect(self, instance):
        print "STREAM CONNECTED %s " % instance
        if getattr(self, 'new_producer', None):
            self.producer.disconnect()
        self.producer = instance

    def onTrackChange(self):
        print "TRACKS UPDATED"
        self.new_producer = self._open_stream()

    def _open_stream(self):
        s = Stream(onStreamConnect)
        s.start()
        return s

if __name__ == "__main__":
    c = Consumer()
This works great when I have just one stream object, and the script closes cleanly. However, when I call my onTrackChange() method and establish more than one connection, the original Stream thread never closes and the run loop doesn't seem to end. It seems whenever I open more than one connection, something is happening to keep the older connection open despite attempting to close it.

I'm sure I'm missing something obvious, but why is it I can't shut down my original stream thread?

Stabby McDamage
Dec 11, 2005

Doctor Rope
I had a crazy idea today. I have a well-defined API that is machine readable and very detailed. I'm wondering if it's possible to translate it into a class dynamically...i.e. to have code that parses the spec and produces a class (not an object) that can be instantiated against it.

Leaving aside the OO aspect for the moment, I'm wondering if there's a programmatic way to construct a function. For example, if I have an array of input variable names (strings) and an array of booleans that indicates if a given input is optional (i.e., defaults to None), could I programmatically build the function? Assume that the body of the function is always the same (it just walks the args and does something). Could it be done? Example:

code:
function_name = 'push_widget'
function_parameters =     ['widget_name','distance','backwards']
function_param_optional = [False        ,False     ,True       ]
funtion_body = generic_handler

# do stuff that causes the equivalent of:
# def push_widget(widget_name, distance, backwards=None):
#     generic_handler(widget_name, distance, backwards)
Obviously I could use string processing to write the python itself, but I think it's more interesting to build the objects dynamically, that way the machine-readable spec can live right in the code.

EDIT: I should add that part of the goal is parameter name checking, so push_widget=generic_handler isn't going to cut it unless we can layer parameter name checking on top somehow.

Stabby McDamage fucked around with this message at 22:26 on May 16, 2011

brosmike
Jun 26, 2009
So, to clarify, you want to REQUIRE callers to use keyword arguments for every parameter, regardless of whether it's optional? That is, something like:

code:
#valid:
mygeneratedfunction(myfirstparam=a, mysecondparam=b)

#not valid:
mygeneratedfunction(a, b)
If that's the case, you use do something like:

code:
def generate_function(param_names, param_optional, body_function):
    def impl(**kwargs):
        num_used_params = 0
        params = []
        # Find the parameters we want to pass on in the order we want to pass them
        for param, optional in zip(param_names, param_optional):
            if param in kwargs:
                num_used_params += 1
                params.append(kwargs[param])
            elif optional:
                params.append(None)
            else:
                raise ValueError("Parameter {} is required".format(param))
                    
        # Make sure there weren't any we don't want
        if num_used_params != len(kwargs):
            invalid_params = [p for p in kwargs.keys() if p not in param_names]
            raise ValueError("The following parameters are invalid: {!s}".format(invalid_params))
            
        #Pass it on to the real implementation
        return body_function(*params)
    return impl
Disclaimer: I haven't tested this. But it should give you the right general idea.

Edit: This doesn't add your function to the global namespace on its own, of course - it's probably cleaner to add it to a dict from function names to these generated functions than to just shove them in the global namespace, unless there's some reason you absolutely can't do that.

brosmike fucked around with this message at 23:27 on May 16, 2011

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"

Stabby McDamage posted:

I had a crazy idea today. I have a well-defined API that is machine readable and very detailed. I'm wondering if it's possible to translate it into a class dynamically...i.e. to have code that parses the spec and produces a class (not an object) that can be instantiated against it.
Use eval() -- it's ugly, not not as ugly as mucking around in internal Python implementation details.

brosmike
Jun 26, 2009

Janin posted:

Use eval() -- it's ugly, not not as ugly as mucking around in internal Python implementation details.

I wouldn't really call using **kwargs "mucking around in internal Python implementation details". It's possible that an eval call could open up a pretty serious security hole, depending on where the data is coming from. Also,

Stabby McDamage posted:

Obviously I could use string processing to write the python itself, but I think it's more interesting to build the objects dynamically, that way the machine-readable spec can live right in the code.

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"

brosmike posted:

I wouldn't really call using **kwargs "mucking around in internal Python implementation details". It's possible that an eval call could open up a pretty serious security hole, depending on where the data is coming from. Also,
The code you posted does not do what he's asking for. What he's asking for requires either eval(), or manually constructing a function object with the proper parameter metadata.

Given how simple the requirements are, it's trivial to verify the input (parameters, function name, and function body match ^[a-zA-Z_]$) before compiling the generated string.

e: Here's a simple implementation. I assumed [[ funtion_body = generic_handler ]] was not a typo, and the attribute is an actual function; if it was a typo, you'll have to tweak this code slightly.

code:
import re
_valid_re = re.compile('^[a-zA-Z_]+$')
def valid(*names):
        for name in names:
                if not _valid_re.match(name):
                        raise ValueError("%r is not a valid function or parameter name" % name)

def construct_function(name, parameters, optional, body):
        valid(name, *parameters)
        param_chunks_1 = []
        param_chunks_2 = []
        for param, param_optional in zip(parameters, optional):
                param_chunks_1.append(param)
                if param_optional:
                        param_chunks_2.append(param + "=None")
                else:
                        param_chunks_2.append(param)
        
        capture = {'function_body': body}
        source = "def %s(%s): function_body(%s)" % ( 
                name,
                ",".join(param_chunks_2),
                ",".join(param_chunks_1))
        exec source in capture
        return capture[name]

TOO SCSI FOR MY CAT fucked around with this message at 04:15 on May 17, 2011

tef
May 30, 2004

-> some l-system crap ->
http://docs.python.org/library/ast.html

Hughlander
May 11, 2005

Where can one find documentation on the C API for writing extensions? I'm trying to do something that I'd think would be pretty easily but I'm having a hard time finding one place that will tell me how to do it.

I want to create a new object, I want to add attributes to it, I want those attributes to be ASCII strings, numbers, lists, and dictionaries. I want to make sure the ref counts are all correct, and return it back to Python.

It seems like this should all be Baby's First Extension, but my google-fu is failing horribly.

tef
May 30, 2004

-> some l-system crap ->
you may enjoy using ctypes or cython instead of the c api

Captain Capacitor
Jan 21, 2008

The code you say?

Hughlander posted:

Where can one find documentation on the C API for writing extensions? I'm trying to do something that I'd think would be pretty easily but I'm having a hard time finding one place that will tell me how to do it.

I want to create a new object, I want to add attributes to it, I want those attributes to be ASCII strings, numbers, lists, and dictionaries. I want to make sure the ref counts are all correct, and return it back to Python.

It seems like this should all be Baby's First Extension, but my google-fu is failing horribly.

I learned all of my extension writing from this tutorial. It's only relevant for the 2.* series, I believe.

The Python docs cover creating extensions here.

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

So I have a bunch of filenames in the wrong character encoding. It shows up like garbage in Windows, etc. Basically they're encoded as big5 but everything thinks they're encoded as utf-8, and I'd like to write a script to go through and re encode them as utf-8 in actuality. I can do what I want manually in the python shell by getting the filename and doing this:

code:
>>> print f
¦Ð¤òªe
>>> f
u'\xa6\xd0\xa4\xf2\xaae'
>>> '\xa6\xd0\xa4\xf2\xaae'.decode('big5')
u'\u7fbd\u6bdb\u6cb3'
>>> print u'\u7fbd\u6bdb\u6cb3'
&#32701;&#27611;&#27827;
From there I'm fine, but the problem is I can't figure out a way to automate it. If I just try f.decode('big5') it says "UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)".

So basically, I need what's actually written on my screen, u'\xa6\xd0\xa4\xf2\xaae', without the u at the front.

Any help? I fukken hate character encodings

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"

MeramJert posted:

So I have a bunch of filenames in the wrong character encoding. It shows up like garbage in Windows, etc. Basically they're encoded as big5 but everything thinks they're encoded as utf-8, and I'd like to write a script to go through and re encode them as utf-8 in actuality. I can do what I want manually in the python shell by getting the filename and doing this:

code:
>>> print f
¦Ð¤òªe
>>> f
u'\xa6\xd0\xa4\xf2\xaae'
>>> '\xa6\xd0\xa4\xf2\xaae'.decode('big5')
u'\u7fbd\u6bdb\u6cb3'
>>> print u'\u7fbd\u6bdb\u6cb3'
&#32701;&#27611;&#27827;
From there I'm fine, but the problem is I can't figure out a way to automate it. If I just try f.decode('big5') it says "UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)".

So basically, I need what's actually written on my screen, u'\xa6\xd0\xa4\xf2\xaae', without the u at the front.

Any help? I fukken hate character encodings
code:
>>> f
u'\xa6\xd0\xa4\xf2\xaae'
>>> f.encode("iso8859-1").decode("big5")
u'\u7fbd\u6bdb\u6cb3'
The trick is that the first 256 Unicode codepoints correspond to ISO-8859-1. You could also do something with chr() and ord(), but that might be a bit more fragile.

Dren
Jan 5, 2001

Pillbug
MeramJert, you might try this

code:
unistr = big5str.decode('big5')
unistr.encode('ascii', errors='replace')
# or one of these
unistr.encode('ascii', errors='ignore')
unistr.encode('ascii', errors='xmlcharrefreplace')
The default option is 'strict', which throws a UnicodeEncodeError.

http://docs.python.org/howto/unicode.html

I had a very similar problem this weekend.

Gothmog1065
May 14, 2009
Okay, posting this code, I know it's completely wrong somehow, but I'm having a complete blank on how to get poo poo to work properly. I'm almost embarrassed to post this, but I'm not going to learn. I gotta be stupid sometime.

pre:
# Guess my number, GUI edition

from tkinter import *

class Application(Frame):
    """ Set up the GUI """
    def __init__(self, master):
        import random
        super(Application, self).__init__(master)
        self.grid()
        self.counter = 0                    #Counter for tries
        self.guess = ""                     #Current Guess
        self.number = random.randint(1, 100)     #Random number
        self.create_widgets()


    def create_widgets(self):
        """ Create the input box """
        self.inst_lbl = Label(self, text = "Guess the number between 1 and 100!"
                              ).grid(row = 0, column = 0, columnspan = 2, sticky = W)
        self.count_lbl = Label(self,
                               text = str(self.counter),
                               ).grid(row = 1, column = 3, sticky = W)
        self.current_guess = Label(self,
                                   text = "Guess: "
                                   ).grid(row = 2, column = 0, sticky = W)
        self.guess = Entry(self)
        self.guess.grid(row = 2, column = 1, sticky = W)

        Button(self,
               text = "Guess!"
               command = self.check()
               ).grid(row = 2, column = 3, sticky = W)
        

    def check(self, guess):
        #Stuff here to check if it's right or not

    def reset(self):
        import random
        self.counter = 0
        self.guess = ""
        self.number = random.randint(1, 100)

root = Tk()
root.title("Guess my number!")
app = Application(root)
root.mainloop()
I know my biggest problem here is how the buttons work, I'm just not able to get my mind around how it works. I'm using Tkinter because it what the book I'm using uses. I might go ahead and do the next chapter, but it really doesn't have much in the way of buttons (pictures and sounds yay!).

Modern Pragmatist
Aug 20, 2008

Gothmog1065 posted:

I know my biggest problem here is how the buttons work, I'm just not able to get my mind around how it works. I'm using Tkinter because it what the book I'm using uses. I might go ahead and do the next chapter, but it really doesn't have much in the way of buttons (pictures and sounds yay!).

I can't actually get this to run, but from the looks of it you are defining the Button callbacks (command) incorrectly. You want to specify a method for the button to call. As a result, you don't want to call the method in the assignment.

command=self.check instead of command=self.check()

Also, it looks like you are trying to pass arguments to the callback. For that you need to use lambda functions. See documentation here

command=lambda: self.check(guess)

Modern Pragmatist fucked around with this message at 16:39 on May 19, 2011

Gothmog1065
May 14, 2009

Modern Pragmatist posted:

I can't actually get this to run, but from the looks of it you are defining the Button callbacks (command) incorrectly. You want to specify a method for the button to call. As a result, you don't want to call the method in the assignment.

command=self.check instead of command=self.check()

Also, it looks like you are trying to pass arguments to the callback. For that you need to use lambda functions. See documentation here

command=lambda: self.check(guess)
Yeah, I'm doing something wrong, all of those are throwing syntax errors.

edit: changed that button to:

pre:
Button(self,
       text = "Guess!"
       command = lambda: self.check(guess)
       ).grid(row = 2, column = 3, sticky = W)

Adbot
ADBOT LOVES YOU

Modern Pragmatist
Aug 20, 2008

Gothmog1065 posted:

Yeah, I'm doing something wrong, all of those are throwing syntax errors.

edit: changed that button to:

pre:
Button(self,
       text = "Guess!"
       command = lambda: self.check(guess)
       ).grid(row = 2, column = 3, sticky = W)

Commas between arguments?
code:
Button(self,
       text = "Guess!",
       command = lambda: self.check(guess)
       ).grid(row = 2, column = 3, sticky = W)

  • Locked thread