Python Forum
How can I make this function faster?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I make this function faster?
#6
(Jun-29-2018, 04:05 PM)volcano63 Wrote: How about writing proper Python code?

How about not being an arrogant [CENSORED BY MOD]? If you have nothing nice to say, get off of my thread. I don't need people like you spreading negativity. I started coding Python two days ago. I'm still learning.

(Jun-29-2018, 03:30 PM)ichabod801 Wrote: For one thing, you're testing original within the loop. I would think it would be faster to check original once, and only loop if it's a dict. Also, is it common or rare that it's not a dict? If it's common, and if statement is generally faster; if it's rare, a try/except block is generally faster. And (not familiar with yaml) what is it if it's not a dict? Could testing for that be faster? The same applies for your index error. I would guess not creating a separate variable fro num would be faster, but probably not significantly.

Great questions. So, let me give some examples. Let's say we have a .YML with random data.

[Image: gjKcN3xGQ96g82lRIp2gCg.png]

If someone uses the read function as such: read("date") then our job is pretty simple. All we need to do is return yaml.safe_load(raw).get("date") However, let's say that I want to get python-version. I would need to do: yaml.safe_load(raw).get("settings.python-version"). Unfortunately, this does not work by default. So, how can we get around this? I begin by splitting their input: value.split(".") and assigning this array to the variable separate. We loop through the array. If the current looped element is not of type dict, then we are finished and we can return. Otherwise, we must append the function like such: yaml.safe_load(raw).get("settings").get("python-version") Using your advice, I changed the try/catch to an if-statement. This now makes the code look much cleaner. So thanks. :)

# Reading data from a file.
def read(value):
    with open(file) as raw:
        reader = yaml.safe_load(raw)

    separate = value.split(".")
    original = reader.get(separate[0])

    for v, value in enumerate(separate, 1):

        if v == len(separate):
            return original

        original = original.get(separate[v])

    return original
Reply


Messages In This Thread
How can I make this function faster? - by Brennan - Jun-29-2018, 08:13 AM
RE: How can I make this function faster? - by Brennan - Jun-29-2018, 06:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make global list inside function CHANKC 6 3,338 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,678 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  how to make my function be a generator Skaperen 2 2,127 Jan-27-2020, 01:07 AM
Last Post: Skaperen
  How to make a function for auto generated id sunnyarora 2 3,041 May-03-2019, 12:47 PM
Last Post: Yoriz
  How can I make a faster search algorithm pianistseb 19 6,981 Apr-18-2019, 05:48 PM
Last Post: Larz60+
  To make an algorithm work faster pianistseb 3 2,942 Apr-01-2019, 08:42 AM
Last Post: Gribouillis
  Rewrite a function to make it work with 'bottle-pymysql' nikos 1 2,085 Feb-26-2019, 02:59 PM
Last Post: nikos
  Make a Function from Name String jge047 4 3,474 Dec-18-2017, 05:05 AM
Last Post: Terafy
  make a list of string in capital by using a function anancba 3 3,577 Nov-23-2017, 06:42 AM
Last Post: heiner55
  How do I make this code run faster? DontHurtMe 0 2,508 Nov-04-2017, 12:12 PM
Last Post: DontHurtMe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020