Python Translate

Posted on
Wed Sep 23, 2020 6:38 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Python Translate

Within the plugin class, translate works as expected.

Code: Select all
a = "This is a sentence with some vowels in it."
indigo.server.log(u"{0}".format(a.translate(None, 'aeiou')))
Code: Select all
Ths s  strng wth sm vwls n t.
But within a class separate from the plugin class, translate throws an error.

Code: Select all
TypeError: translate() takes exactly one argument (2 given)
The line throwing the error is:

Code: Select all
return r"#{0}".format(c.translate(None, ' '))
What am I not seeing?

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Sep 23, 2020 9:35 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Python Translate

DaveL17 wrote:
What am I not seeing?

The vowels?

Seriously, I get lost moving between classes at the best of times, but whenever I see that error I usually add or remove ‘self’ as the first parameter.

In your case, you’re calling translate(None,’ ‘) so my first port of call would be to remove None.

Secondly, I’m confused how translate is even working because it takes a trans object using maketrans() - speaking randomly, do you need to provide ‘ ‘ as a list/array/dict of conversions, even from ‘ ‘ to ‘’

Thirdly, have you unwittingly defined your own translate() function within this class that you are accidentally calling?

Peter


Sent from my iPhone using Tapatalk Pro

Posted on
Wed Sep 23, 2020 10:27 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Python Translate

Seriously, I get lost moving between classes at the best of times, but whenever I see that error I usually add or remove ‘self’ as the first parameter.

You sound like you are guessing there, Howard. LOL

Is there any chance that your other (non-working) call is running in another process and picking up a different Python version? I *believe* they changed how that works and possibly the function signature between versions. Don't quote me on that, but I believe that was listed in a site mentioning some differences between the versions when I looked (in case Indigo goes to a new version as they have pondered here on the forums).

If you are just trying to remove characters, you could always do that with a regular expression as well.

Posted on
Thu Sep 24, 2020 4:49 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python Translate

Thanks to both of you for chiming in. I'm sure that I haven't defined my own translate function. I shouldn't think it's a 'self' issue since I'm just calling a standard (base) Python library function. What I'm attempting to do is clean up code by using translate instead of chained replace() calls. This:

Code: Select all
a = "This is a sentence with some vowels in it."
indigo.server.log(u"{0}".format(a.translate(None, 'aeiou')))
Not this:

Code: Select all
a = "This is a sentence with some vowels in it."
indigo.server.log(u"{0}".format(a).replace('a', '').replace('e', '').replace('i', '').replace('o', '').replace('u', ''))
But I think Adam is onto something that may explain some other weird behavior I've been seeing. These calls *do* run in another process (multiprocessing.process) and the machine I'm developing on is running Catalina (so it may be picking up Python 3). In fact, I bet that's exactly what's happening.

I'll do some more research to see if it's possible to force the right Python version in a subprocess. This will need to be addressed in a couple of my plugins! :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Sep 24, 2020 7:45 am
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Python Translate

What I'm attempting to do is clean up code by using translate instead of chained replace() calls.

Although you may have some desire to fix the versioning issue for other reasons as well, Regular Expressions are seriously powerful text manipulation tools that can do what you want (easy replace without chained calls). It is something like this for your vowel removal example:

Code: Select all
import re
a = "This is a sentence with some vowels in it."
indigo.server.log(u"{0}".format(re.sub("[aeiou]", " ", a))

Regular Expressions can get really, really, really complicated... but the basics are pretty easy. In this particular example, any individual characters in the brackets will match and be replaced with the second param.

Adam

Posted on
Thu Sep 24, 2020 8:37 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python Translate

Thanks. I use regular expressions in a couple of places, but I use them so infrequently, I can never remember the structure. That's *probably* what I'll end up using as I think it would be a more future-proof method than translate--especially given the following...

I haven't completely figured out the issue yet, but I think this was masquerading as a class issue (or a subprocess/Python version issue) but now I'm leaning more towards it being a translate issue as the structure of calls is different with Python 2.6/2.7/3.X. The following structure is no longer throwing an error and passing the translation table directly seems to work.

Code: Select all
r"#{0}".format(c.translate({35: None, 32: None}))
Before: '88 88 88'
After: '#888888'

(The reason I remove and re-add the hash is because sometimes the before value includes it and sometimes it doesn't. This keeps me from getting '##888888'.)

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests

cron