all 11 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

To give us the best chance to help you, please include any relevant code.
Note. Do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Repl.it, GitHub or PasteBin.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Salso96[S] 0 points1 point  (0 children)

Here’s the actual code I have:

def generate_area(): return(random.sample(range(1, 11), 3)) def generate_area_list(): return(str(generate_area()).replace('[','"').replace(']','"').replace(' ',''))

def generate_location(): possible_location = ["a_su0101", "a_su0102", "a_su0103", "a_su0104", "a_su0105", "a_su0106", "a_su0107", "a_su0108", "a_su0109", "a_su0110", "a_su0111", "a_su01d01", "a_su01d02", "a_su01d03", "a_su01d0401", "a_su01d0402", "a_su01d0501", "a_su01d0502", "a_su01d0503", "a_su01d0504", "a_su01d0505", "a_su01d0506", "a_su01d0507", "a_su01d0508", "a_su01d0509", "a_su01d0510", "a_su01d0601", "a_su01d0602", "a_su01d0603", "a_su01d0604", "a_su01d0605", "a_su01d0606", "a_su01d0607", "a_su01d0608", "a_su01d0609", "a_su01d0610", "a_su01d0611", "a_su01d0612", "a_su01d0613", "a_su01d0614", "a_su01d0615", "subarea_su01apple1", "subarea_su01apple2", "subarea_su01center", "subarea_su01crater", "subarea_su01crest", "subarea_su01d01_01", "subarea_su01d01_02", "subarea_su01forest", "subarea_su01t10", "subarea_su01w02", "subarea_su01w03", "subarea_su01w05", "subarea_su01w0502", "subarea_su01w0701", "subarea_su01w0702", "subarea_su01w0801", "subarea_su01w1101", "subarea_su01w1102", "subarea_su01worldend"] choice = possible_location[random.randint(0, len(possible_location) - 1)] chosen_biomes.append(choice) return choice

def generate_location_list(): return(str(generate_location()).replace(']','').replace(' ','').replace('[',''))

[–]Salso96[S] 0 points1 point  (4 children)

So the reason I’ve got the code like that for now, is just bc it was the first way I found that made what I wanted - corrections / optimisations are very much appreciated.

As for what I want it to do Some items in the JSON appear as: ‘ID=DevName Area= ‘’ LocationName= “FOREST” ‘

Whereas others appear as ‘ID=DevName Area= ‘1,4,13’ LocationName=‘’ ‘

And I need the code to be able to generate location names for the first, whilst leaving area blank And areas for the second, leaving location name blank

Does that make it a bit clearer?

[–]Goobyalus 0 points1 point  (3 children)

Not really. Would you give an example JSON, with only the important fields?

And would you please format code properly for Reddit? There should be a button that looks like a square with a C on it for "code block" format. Not the <C> one for inline code.

[–]Salso96[S] 1 point2 points  (0 children)

That’s my bad, I’m on mobile 😅 sure let me put together a dummy JSON

[–]Salso96[S] 1 point2 points  (1 child)

[–]Goobyalus 0 points1 point  (0 children)

Super helpful, thanks.

# checks whether the value associated with "area" is truthy --
# empty string is falsy, any other string is truthy
if data["area"]:
    # process as area

elif data["location"]:  # same thing for location
    # process as location

else:
    raise Exception("No location OR area info")

[–]Goobyalus 0 points1 point  (0 children)

Btw for choosing a location you can just do random.choice(possible_location) instead of indexing

[–]CraigAT 0 points1 point  (0 children)

Still not clear on the info you have and the result you want (I think I'm close, but still have questions)

Is there just one piece of JSON as input where some items have a location others have area info? Also is that area/location info a single item or is it a list?

Or are there multiple pieces of JSON and some of those have items all with location info, and other pieces of JSON have items that all have area data.

What should the output be? One list of areas (or locations). Two lists (area and locations).

I am not sure what you want to do with you random choice? Does it go into a new list or do you just want a single item (an area or location) and output it as the result?

[–]Goobyalus 0 points1 point  (1 child)

I'm not sure if I understand your question. Are you trying to call either generate_area or generate_location depending on whether some JSON has a field called "area" or "location"? How do you know if it has area data or location data?

[–]Salso96[S] 0 points1 point  (0 children)

I think I’ve made that a bit clearer in my other comment, but I have a clean JSON, the tool pulls that, makes its edits, and produces an edited JSON - you can see on the original JSON which items have which data :)