Ask Your Question
1

How to deal with unicode strings - PSS/e 33, Python 2.7, and wxPython

asked 2013-09-24 15:03:15 -0600

EBahr gravatar image

It seems there are various PTI PSS/e python modules that aren't very friendly with unicode formatted strings (psse_env_manager,dyntools, etc). I never had this issue with Rev 32 and the previous bundled python utilities, but the new version is causing some issues with some of my programs. I believe the issue stems from the new wxPython module that is unicode rather than ascii. Anytime I grab text from any one of my GUI's, it converts the text string to unicode format (ie u"string"). Often, I grab a portion of a string from a GUI to define something like a dynamic channel file name. Any time I even grab a portion of the original text and manipulate it, it still formats it as unicode. So if I specify a file in unicode format in psse_env_manager or dyntools, the packages straight up ignore the text and either do nothing (dyntools) or throw an error (psse_env_manager).

So, what I am wondering is if any of you all have ever dealt with this issue? I feel I have two options, both of which are less than ideal:

  1. Convert all text gathered from TextCtrl, ListCtrl, etc boxes into strings (str(object.GetValue())) in every single GUI, or
  2. Go through code and make sure all psse module function calls have an appropriately encoded string (non-unicode) in them

Any ideas?

Is it possible to force ASCII/utf-8 encoding in my programs to disable it ever encoding a string as unicode?

Is it possible to build a function to loop through local/global variables and convert any unicode string to a general string?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-09-25 14:23:45 -0600

jsexauer gravatar image

I have never heard of this problem, but just shooing from the hip, I think this will solve your problem. This creates a decorator which inspects all the inputs to calls to functions in the dyntools module and takes those which are unicode and converts them to strings.

import types
def UnicodeToStrDecorator(psseFunc):
    def callPsseFunc(*args, **kwargs):
        newArgs = []
        newKwargs = {}
        # Sanitize args and kwargs for unicode
        for a in args:
            if type(a) is unicode:
                newArgs.append(str(a))
            else:
                newArgs.append(a)

        for k, v in kwargs.items():
            if type(v) is unicode:
                newKwargs[k] = str(v)
            else:
                newKwargs[k] = v

        # Call origional psse function with new, sanitized arguments
        return psseFunc(*newArgs, **newKwargs)
    return callPsseFunc

import dyntools, psse_env_manager 

# Apply decorator the dyntools module
for k,v in vars(dyntools).items():
    if isinstance(v, types.FunctionType):
        vars(dyntools)[k] = UnicodeToStrDecorator(v)

# Apply decorator to psse_env_manager module
for k,v in vars(psse_env_manager).items():
    if isinstance(v, types.FunctionType):
        vars(psse_env_manager)[k] = UnicodeToStrDecorator(v)
edit flag offensive delete link more

Comments

2

Wow, works perfectly. It looks like even `psspy` has an issue with unicode strings, so I think this is an issue that PTI needs to fix eventually. I think this decorator will work perfectly in the mean time though. Thanks!

EBahr gravatar imageEBahr ( 2013-09-25 14:58:27 -0600 )edit

sorry where should i add this code to solve my problem like you

M10807115 gravatar imageM10807115 ( 2020-01-20 02:08:59 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

Stats

Asked: 2013-09-24 15:03:15 -0600

Seen: 1,896 times

Last updated: Sep 25 '13