2

I have the following function which produces results;

myNames = ['ULTA', 'CSCO', ...]

def get_from_min_match(var):
    temp = []
    count_elem = generate_elem_count()
    for item in count_elem:
        if var <= count_elem[item]:
            temp.append(item)
    return set(temp) if len(set(temp)) > 0 else "None"

def generate_elem_count():
result_data = []
for val in mapper.values():
    if type(val) == list:
        result_data += val
    elif type(val) == dict:
        for key in val:
            result_data.append(key)

count_elem = {elem: result_data.count(elem) for elem in result_data}
return count_elem

I call this function like this;

myNames_dict_1 = ['AME', 'IEX', 'PAYC']
myNames_dict_1 = ['ULTA', 'CSCO', 'PAYC']

mapper = {1: myNames_dict_1, 2: myNames_dict_2}
print(" These meet three values ", get_from_min_match(3))
print(" These meet four values ", get_from_min_match(4))

The output I get from these functions are as follows;

These meet three values {'ULTA', 'CSCO', 'SHW', 'MANH', 'TTWO', 'SAM', 'RHI', 'PAYC', 'AME', 'CCOI', 'RMD', 'AMD', 'UNH', 'AZO', 'APH', 'EW', 'FFIV', 'IEX', 'IDXX', 'ANET', 'SWKS', 'HRL', 'ILMN', 'PGR', 'ATVI', 'CNS', 'EA', 'ORLY', 'TSCO'}
These meet four values {'EW', 'PAYC', 'TTWO', 'AME', 'IEX', 'IDXX', 'ANET', 'RMD', 'SWKS', 'HRL', 'UNH', 'CCOI', 'ORLY', 'APH', 'PGR', 'TSCO'}

Now, I want to insert the output, of the get_from_min_match function into a Sqlite database. Its structure looks like this;

dbase.execute("INSERT OR REPLACE INTO min_match (DATE, SYMBOL, NAME, NUMBEROFMETRICSMET) \
VALUES (?,?,?,?)", (datetime.today(), symbol, name, NUMBEROFMETRICSMET?))
dbase.commit()

So, it's basically a new function to calculate the "NUMBEROFMETRICSMET" parameter rather than calling each of these functions many times. And I want the output of the function inserted into the database. How to achieve this? Here 3, 4 would be the number of times the companies matched.

date ULTA name 3
date EW name 4
...

should be the result.

How can I achieve this? Thanks!

Zac1
  • 208
  • 7
  • 34
  • 1
    The question and answers [here](https://stackoverflow.com/q/71322371/5320906) may help. – snakecharmerb Apr 12 '22 at 18:07
  • Did you look at the examples in the documentation? [https://docs.python.org/3/library/sqlite3.html](https://docs.python.org/3/library/sqlite3.html) – wwii Apr 12 '22 at 19:02
  • Can you clarify what you want to do? What is wrong with just dumping the result to JSON and writing the result to a text column? – snakecharmerb Apr 15 '22 at 06:48
  • @snakecharmerb Hi. I don't know how to calculate the "numberofmetricsmet" parameter, for each value in an array called myNames. Here's what I am thinking; for each item in myNames, If item exists in 1match, enter in database If item exists in 2match, enter in database If item exists in 3match, enter in database If item exists in 4match, enter in database If item exists in 5match, enter in database If item exists in 6match, enter in database else, enter in database as None matched But, I need a more efficient way than calling the function so many times. – Zac1 Apr 15 '22 at 20:54
  • @snakecharmerb I edited the question to add more details. Can you please check now. – Zac1 Apr 15 '22 at 21:04
  • @wwii I edited the question to add more details. Can you please check now. – Zac1 Apr 15 '22 at 21:04

1 Answers1

1

I fixed this by just using my already written function;

count_elem = generate_elem_count()
print("Count Elem: " + str(count_elem))

This prints {'AMPY': 1} and so on.

Zac1
  • 208
  • 7
  • 34