Help cleaning code

Hi!

def funct(json):

	if json.get("tagPath"):
		tag_path = json.get("tagPath") + "/" + json.get("name")
		base_path = "[" + conf.default_prov + "]" + json.get("tagPath")
	else:
		tag_path = json.get("name")
		base_path = "[" + conf.default_prov + "]"

Any idea on doing this in a cleaner way?
Not liking much the double if

I only see one if/else? Which isn’t bad or complex or something that tells me code immediately needs to be refactored.

Regardless, I am partial to string formatting now via .formatso I would write this
base_path = "[" + conf.default_prov + "]"

like

base_path = "[{default}]".format(default=conf.default_prov).

You could use a ternary operator if you like those though it might be very wordy and I am not sure it actually makes it cleaner.

tag_path = ‘{tag_path}/{name}’.format(tag_path=json.get(‘tagPath’), name=json.get(‘name’) if json.get(‘tagPath’) else json.get('name')

Is your else clause correct? You define tag_path but then don’t use it. How do they get combined at the end? Feels like you could probably do your tag_path and base_path in ternary operators but again - I don’t think that always makes code cleaner or more readable.

Only other thing I would say - pick one, snake_case or camelCase, it looks like you use both.

1 Like

I would do something like this:

def funct(json):
    tagPath = json.get('name')
    basePath = '[{}]'.format(conf.default_prov)

    if json.get('tagPath'):
        tagPath = '{}/{}'.format(json.get('tagPath'),tagPath)
        basePath += json.get('tagPath')

Also, I prefer camelCase in jython, because it is the Java standard and because snake_case makes my eye twitch. It doesn't really matter, but it helps with continuity when you're using Java classes, plus that is the standard that IA uses with their scripting functions.

3 Likes

Thx for you answer, it actually helped me decide on how to do it!

About the different types, we use snake_case, but I have to use camelCase because I'm working with tags, and the tags use camelCase. So all my code is snake_case except for the values on the dictionary that are meant to work with tags.

Throwing my hat in the ring (this does something slightly different to your initial sample, in that it always appends name to the path, but that seems likely to be what you want anyways):

def funct(json):
	pathParts = [
		json.get("tagPath"),
		json.get("name"),
	]

	path = "[{provider}]{path}".format(
		provider=conf.default_prov, 
		path="/".join(filter(None, pathParts))
	)
3 Likes

What would be the point of applying a filter of None? Is that just a placeholder you create out of habit?

It filters out None values, so if json.get(‘tagPath’) returns None it’s not in the final list that gets joined via ’/’

4 Likes

Ah, okay, I've never used it so I misunderstood it to mean "don't filter anything"...

1 Like

It’s a special case that was included presumably because its such a common operation. It removes all fasly values , 0, None, ‘‘, False, {} etc

1 Like

This is why I keep browsing these forums. Now that I know I'll likely ending up using it myself at some point...

3 Likes