Generator instead of list comprehension? And where else can I improve my class?

I am new to Python. I have been studying the concept of python classes recently. Well, for a couple of the scripts I wrote, I thought it would be convenient to define a class that combines the SQL statement (in the ArcGis dialect). Nothing special. Here is what I came up with. I ask two things: first, common flaws? Suggestions for improvement? Secondly, I got a bit stuck in the code of the last def constructor function. In fact, I want to return a tuple from the dictionary, not a list. But understanding the list is nice. So what about the generator? I can’t figure out exactly how to do this ...

 class ArcSql: type_book = {'str':("'","'"), 'int':("", "")} format_book = dict(shp=("'","'"), GDB=("[","]")) def __init__(self,colom_name, values_list, value_type = 'str', arc_format ='shp'): self.colom = colom_name self.values = values_list self.valtype = self.constructor(type_book, value_type) self.aformat = self.constructor(format_book, arc_format) self.colom_formated = str(self.aformat[0][0]) + self.colom + str(self.aformat[0][1]) def statement(self): temp_state = [] connector = "'OR' " count_values = len(self.values) if count_values == 0: return("error, not enough values...") else: for v in self.values: x = self.colom_formated + " = " + str(self.valtype[0][0]) + v + str(self.valtype[0][1]) + ' ' + connector temp_state.append(str(x)) state = "".join(temp_state)[:-5] return(str(state)) def constructor(self, book, book_key): return([v for k,v in book.iteritems() if k==book_key]) 
+4
source share
3 answers

Your first question should be better asked on the codereview website .

As for your second question, you can use the generator as follows:

 def constructor(self, book, book_key): for k, v in book.iteritems(): if k==book_Key: yield v ... for value in obj.constructor(book ,book_key): # Do whatever you need with value 

In any case, I understand that you will get only one result from understanding the list, in this case:

 def constructor(self, book, book_key): return next(v for k, v in book.iteritems() if k==book_key, None) ... value = obj.constructor(book ,book_key) if value is not None: # Do whatever you need with value 

Where next is built-in to return the next iterator value or the default value ( None in this case), if the iterator is exhausted (if the default value is not passed, an exception will be thrown in this case).

+4
source

You can easily change your understanding of the list into a generator by simply replacing [] with () to make it a generator, and replacing return with the yield generator keyword:

 yield (k,v in book.iteritems() if k==book_key) 

Now you need to return only one tuple - if you want more (like a list), you need to call list(constructor()) .

+3
source
 def constructor(self, book, book_key): return([v for k,v in book.iteritems() if k==book_key]) 

If book is a dictionary, its keys are unique. So why iterate over this dict to find the value?

Why not:

 def constructor(self, book, book_key): return (book[book_key],) 
+1
source

Source: https://habr.com/ru/post/1398563/


All Articles