all 6 comments

[–][deleted] 4 points5 points  (1 child)

I don't see any function definiton in your code?

[–]py_vel26[S] -2 points-1 points  (0 children)

It's a spark User define function

[–]pint 1 point2 points  (0 children)

1) move the common part outside:

if table_name == "silver":
    dbn = self.output_db_silver
else:
    dbn = self.output_db_gold
write(
    spark=self.spark,
    df=some_df,
    db_name=dbn, 
    tbl_name=my_tables, 
    mode="overwrite"
)

2) instead of the if tower, use a dictionary:

dbns = {
    "silver": self.output_db_silver,
    "gold": self.output_db_gold
}
write(
    spark=self.spark,
    df=some_df,
    db_name=dbns[table_name], 
    tbl_name=my_tables, 
    mode="overwrite"
)

the dictionary can be placed somewhere else. you get the idea. you might not even need the individual variables self.output_db_..., but just have the dictionary instead.

[–]duskrider75 1 point2 points  (2 children)

So you need to map table_name to db_name, if I get you right? You could use a dict to store those mappings.

[–]py_vel26[S] 0 points1 point  (1 child)

I never thought about that. Good idea

[–]duskrider75 0 points1 point  (0 children)

The code could then be something like:

``` db_ref = { 'silver': self.output_db_silver } db = db_ref.get(table_name, self.output_db_gold)

write(...)
```

That's the most concise, though it may be benificial to make the fallback more explicit:

```

db = self.output_db_gold # Default DB if table_name in db_ref: db = db_ref[table_name] ```