Skip to main content

Posts

How to catch a function into a variable and change its code?

If a variable contains an existing function, how can you replace something inside its code? It seems toString() (in order to replace the string) adds an "extra" function() { } and therefore fails. For example changing test1 into test2 or test3 in: var theparent = { myfunc: function () { console.log('test1'); } } console.log(theparent.myfunc); theparent.myfunc(); theparent.myfunc = new Function("console.log('test2')"); // Works console.log(theparent.myfunc); theparent.myfunc(); theparent.myfunc = new Function(theparent.myfunc.toString().replace('test2', 'test3')); // Adds function() { } console.log(theparent.myfunc); theparent.myfunc(); // Fails Via Active questions tagged javascript - Stack Overflow https://ift.tt/WQMG1jy

Extract html and wordpress shortcodes into array of objects

I am working a headless Wordpress site with Nuxt as my front end. The site has thousands of articles which have shortcodes. I am getting all the page data via graphql and I render content using v-html and it is all working great, but the shortcodes obviously only render as plain text. They are mostly very simple shortcodes so I am going to create Vue components to replace them using <component :is="someshortcode"> What I need to do is split my html up into an array of objects I can use to render the parts of the page as html or a component depending what it is. I imagine the best way to do this is with regex which is where I am stumped. Let's say I have the following html with some shortcodes <h1>Lorem ipsum dolor sit amet</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus facilisis elit ante. Vivamus semper dui eget justo viverra facilisis. Etiam ut leo fermentum, sagittis mauris nec, placerat lorem.</p> <h2...

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...

Greedy algorithm visualization

I have a problem which goes as follows. There are n items. Each item i takes m_i time to manufacture and t_i time to test. Obviously, an item must be manufactured before being tested. At any point of time, manufacturing queue and testing queue can have only one item in each of them. Now, the objective is to schedule the items to minimize the total production time. I have written the code for this: # data is a list of lists which will be in the form of [[id, m_i, t_i]] data.sort(key=lambda x: x[1]) # Sort by manufacturing time print(f"Items will be produced in the order: {', '.join(str(x[0]) for x in data)}") idle = 0 # Idle time is the total time when the testing unit will have no work schedule = 0 for drone in data: if last and last <= drone[1]: idle = drone[1] - last + 1 schedule += drone[2] last = schedule + idle print(f"The total production time is {schedule+idle}") Now, for the following input, 1 / 5 / 7 2 / 1 / 2 3 / 8 /...

How to indentify a user's keyboard app on mobile in javascript?

I'm working on a project that I need to change the text input depending on the user's keyboard app like Gboard for example, I need to do it because the text input/text editor (Draft.js) that I'm currently using on desktop has some bugs with the Gboard and I can't seem to fix it. So I need to isolate Gboard user's from the Text Editor to a normal textarea. Is there a way to find out what keyboard app is the user currently using on the site? Via Active questions tagged javascript - Stack Overflow https://ift.tt/WQMG1jy

SQL loop in Snowflake for comparison

I am trying to write this SQL script in Snowflake. So here is a table: tableName count number TableA 23 TableB 19 TableD 0 The table above is a benchmark. It has row counts of each named table (i.e TableA has 23 rows) . I am trying to write a stored proc that loop through and gets a hold of count number (value) then goes back to a different schema where the actual tables are, then select count(*) those table and compare output of each number in the benchmark to actual count. if both counts are equal, copy the table into a new location, else return a message Via Active questions tagged javascript - Stack Overflow https://ift.tt/WQMG1jy

How to loop though array and return specific deep values? using javascript

I need to extract information from deeply nested properties. Check my structure of array. let arr = [ { "key":"test1", "value":{ "versions":{ "variable_value-1-can-be-different-name-always":{ "obj": "412421", "translations":{ "trans1-can-be-variable-name":{ "name": '1' } } } } } }, { "key":"test11/13", "value":{ "versions":{ "variable_value-2-can-be-different-name-always22":{ "obj": "1111", "translations":{ "trans2-can-be-variable-name":{ "name": '2' } } } ...