dry

How can I simplify repetitive if-elif statements in my grading system function?

◇◆丶佛笑我妖孽 提交于 2021-02-04 09:22:30
问题 The goal is to build a program to convert scores from a '0 to 1' system to an 'F to A' system: If score >= 0.9 would print 'A' If score >= 0.8 would print 'B' 0.7, C 0.6, D And any value below that point, print F This is the way to build it and it works on the program, but it's somewhat repetitive: if scr >= 0.9: print('A') elif scr >= 0.8: print('B') elif scr >= 0.7: print('C') elif scr >= 0.6: print('D') else: print('F') I would like to know if there is a way to build a function so that the

How do I listen to mouseover and mouseout events in 3 elements in a more DRY way?

亡梦爱人 提交于 2021-01-28 15:01:16
问题 I'm applying some animations to a navbar through JS but I'm probably repeating myself a lot in the event listening part. But I can't find a way to improve it. Is there a way I can use a loop or something in order to listen to all these events without writing a code block for each one? let headerHome = document.querySelector('.headerHome'); let headerPlayground = document.querySelector('.headerPlayground'); let headerAbout = document.querySelector('.headerAbout'); headerHome.addEventListener(

How do I listen to mouseover and mouseout events in 3 elements in a more DRY way?

最后都变了- 提交于 2021-01-28 14:35:14
问题 I'm applying some animations to a navbar through JS but I'm probably repeating myself a lot in the event listening part. But I can't find a way to improve it. Is there a way I can use a loop or something in order to listen to all these events without writing a code block for each one? let headerHome = document.querySelector('.headerHome'); let headerPlayground = document.querySelector('.headerPlayground'); let headerAbout = document.querySelector('.headerAbout'); headerHome.addEventListener(

Avoid violation of DRY with ternary?

徘徊边缘 提交于 2021-01-28 02:06:43
问题 Here's what I have. Map data = new HashMap<>(); // assume this has been populated public int getLastestVersion() { // data.get("PATH_TO_DESIRED_POINT") would be an integer return data.get("PATH_TO_DESIRED_POINT") == null ? 0 : (int)data.get("PATH_TO_DESIRED_POINT"); } I'm trying to avoid violating DRY, but I want to be able to keep the simplicity of the ternary. Is there something I can do? 回答1: If you are using Java8, you can use getOrDefault method: return data.getOrDefault("PATH_TO_DESIRED

How can I hook into Spring's @RequestBody argument resolving to resolve my own argument using the body of the request

元气小坏坏 提交于 2021-01-28 01:15:29
问题 In the process of developing our app, we found ourselves doing something like this in our controller: @RequestMapping(value = "foo", method = RequestMethod.POST) @ResponseBody public SimpleMasterWidget doStuff(@RequestBody ClientData clientData, ServerData serverData) throws Exception { pushClientDataToServerData(clientData, serverData); //stuff specific to this operation on the serverData return whatever; } The pushClientDataToServerData(clientData, serverData); call gets duplicated in every

How to Apply functions to specific set of columns in data frame in R to replace NAs

我与影子孤独终老i 提交于 2021-01-27 21:50:55
问题 I have a data set in which I want to replace NAs in different columns differently. Following is the dummy data set and code to replicate it . test <- data.frame(ID = c(1:5), FirstName = c(NA,"Sid",NA,"Harsh","CJ"), LastName = c("Snow",NA,"Lapata","Khan",NA), BillNum = c(6:10), Phone = c(1213,3123,3123,NA,NA), Married = c("Yes","Yes",NA,"NO","Yes"), ZIP = c(1111,2222,333,444,555), Gender = c("M",NA,"F",NA,"M"), Address = c("A","B",NA,"C","D")) > test ID FirstName LastName BillNum Phone Married

Suggestions to make this jQuery function more DRY / more efficient

◇◆丶佛笑我妖孽 提交于 2020-08-10 19:14:44
问题 Following previous post the this code works and does the job but I am conscious this is about as DRY as the Pacific on a wet day. I's be grateful for any suggestions that will make it more efficient. $( "#cvl_mb_services .content-switch" ).each(function(index, el) { var parent = $(el).parent().parent().attr("id"); var inputValue = $('#' + parent + ' input[type=radio]:checked').val(); var targetBox = '#' + parent + ' .cvl-' + inputValue + '-fields'; $(targetBox).removeClass('cvl-hide'); }); $(

How to use GraphQL fragment on multiple types

送分小仙女□ 提交于 2020-06-13 19:24:27
问题 I have a Gatsby project with very similar GraphQL queries for two different types of content: regular pages and wiki articles. Page by slug export const query = graphql` query($slug: String!) { page: contentfulPage(slug: {eq: $slug}) { title slug body { remark: childMarkdownRemark { excerpt html headings { value depth } } } updatedAt(formatString: "D. MMM YYYY") authors { name email } } } ` Wiki article by slug export const query = graphql` query($slug: String!) { article:

Ruby on rails DRY strip whitespace from selective input forms

余生长醉 提交于 2020-02-27 12:28:28
问题 I'm fairly new to rails so bear with me. I want to strip whitespace from a selective group of input forms. But I would like a DRY solution. So I was thinking there might be a solution such as a helper method, or a custom callback. Or a combination such as before_validation strip_whitespace(:attribute, :attribute2, etc) Any help is awesome! Thanks! EDIT I have this in my model file ... include ApplicationHelper strip_whitespace_from_attributes :employer_name ... and I have this in my