• Breaking News

    Saturday, April 28, 2018

    ReactJS Time between dates in real time Ask Programming

    ReactJS Time between dates in real time Ask Programming


    ReactJS Time between dates in real time

    Posted: 28 Apr 2018 07:32 PM PDT

    I'm trying to figure out how I can make a real-time counter that will increment the seconds without having to refresh my page. Here is my code below. I'm quite new to using ReactJS any hints/help will be greatly appreciated. Thanks.

    import './Home.css'; import React, { Component } from 'react'; import Moment from 'react-moment';

    export default class Home extends Component { constructor() { super(); this.state.date = new Date(); } updateTime() { var date = this.state.date; this.setState({ date : date.setSeconds( date.getSeconds() + 1 ) }); } componentDidMount() { this.interval = setInterval(this.updateTime, 1000); } componentWillUnmount() { clearInterval(this.interval); } render() { const together = "2017-05-14T06:30"; return ( <div> <p><Moment interval={10} diff={together} unit="seconds">{this.state.date}</Moment> Seconds</p> </div> ); } } 
    submitted by /u/itiszac
    [link] [comments]

    Botnet to order pizza on Silicon Valley?

    Posted: 28 Apr 2018 09:02 AM PDT

    Is that actually possible without obtaining a massive amount of working credit card numbers? In the show it looked like it was a pure programming feat.

    submitted by /u/WSBshitposter
    [link] [comments]

    Scaffolding vs manually configuring your app (Spring + Angular)

    Posted: 28 Apr 2018 11:59 AM PDT

    Recently, I've started experimenting with Spring and Angular.
    I've followed some tutorials and then I've found JHipster.
    So now I have a fully functional skeleton with all the configurations done.

    Now I have two choices:
    1. Starting from scratch and making each functionality by hand
    2. Using the already configured skeleton and changing the app as I go

    What would be best for learning?

    submitted by /u/BittyMitty
    [link] [comments]

    Need to create a bot that tweets new website posts

    Posted: 28 Apr 2018 11:13 AM PDT

    Hello everyone, thanks in advance for reading/advising.

    How would I go about making a bot that scans certain websites, and tweets a URL when there is a new post to that website (think a new news article)

    If there's already a free/paid version of this software that exists, I'd appreciate a link to that as well!

    Thank you!

    submitted by /u/nfg91
    [link] [comments]

    Help trying to figure spacing of Cumulative Trapezoidal integration (MatLab help)

    Posted: 28 Apr 2018 01:14 PM PDT

    I'm using cumtrapz given a list of voltage v and I'm trying to find the equivalent I(t) using this formula.

    integral(vdt) =KI(t) +Cintegration.

    K is a known constant. The voltages come from a rogoski coil. I'll be taking 10,000 samples of v per second. Does that mean X in cumtrapz(X,Y) is 1/10000th of a second also while ideally it would be 10ksps in actuallity it might vary is there a way to handle that or will it probably not be an issue?

    I know this is a pretty electronics heavy programming question if there is a better subreddit I would be willing to move it but it seemed programming specific enough I decided to post here instead of in /r/askelectronics

    submitted by /u/dlrdlrdlr
    [link] [comments]

    multi-threading help

    Posted: 28 Apr 2018 12:49 PM PDT

    Can any one tell me how to make each thread compute a different slice? Am assuming I need to use task queues. But am not quite sure how to implement that. .... .....

    void slicer(double right, double left, int slices) {

    double start = 0, end = 0; const size_t nloop = 5; const size_t nthreads = std::thread::hardware_concurrency(); { // Pre loop std::cout << "parallel (" << nthreads << " threads):" << std::endl; std::vector<std::thread> threads(nthreads); std::mutex critical; for (int t = 0; t < nthreads; t++) { threads[t] = std::thread(std::bind( [&]() { sliceSize = 800 / slices; for (int i = 0; i < slices; i++) { start = i * sliceSize; end = ((1 + i) * sliceSize); compute_mandelbrot(right, left, start, end); } std::lock_guard<std::mutex> lock(critical); })); } std::for_each(threads.begin(), threads.end(), [](std::thread& x) {x.join(); }); } 

    }

    submitted by /u/bllueace
    [link] [comments]

    (Testing) Could someone review whether I am doing this right or not?

    Posted: 28 Apr 2018 10:27 AM PDT

    Hey,

    I'm fairly new to testing. I wrote a simple client, which can open a connection via a WebSocket and gather system informations (for now only read system env. variables).

    This works all fine, however I really struggle with the tests. To be honest, writing the test (doing it with the Test Driven Development approach) took me much more time than writing the productive code. Sure, I'm new to testing so I will learn with time, but I still wanted to mention that.

    So far I have written the folloing two class:

    ConfigurationProvider.java

    @FunctionalInterface public interface ConfigurationProvider { HashMap<String, ArrayList<String>> getEnvVars(ArrayList<String> keys); } 

    Configuration.java

    // Provides the configuration as a HashMap. public class Configuration { private static HashMap<String, ArrayList<String>> cfg = initializeConfiguration((keys) -> { HashMap<String, ArrayList<String>> keyValuePairs = new HashMap<>(); keys.forEach(key -> { String value = System.getenv(key); if (value != null) { ArrayList<String> values = new ArrayList<>(); values.addAll(Arrays.asList(System.getenv(key).split(";"))); keyValuePairs.put(key, values); } }); return keyValuePairs; }, new ArrayList<>(Arrays.asList("BINOCULARIS_SERVER", "BINOCULARIS_FILES"))); // Getter public static HashMap<String, ArrayList<String>> getCfg() { return cfg; } // Uses the functional interface 'ConfigurationProvider' to provide an implementation of method 'getEnvVar'. public static HashMap<String, ArrayList<String>> initializeConfiguration(ConfigurationProvider cfgProvider , ArrayList<String> keys) { return cfgProvider.getEnvVars(keys); } } 

    I only used the ConfigurationProvider class to pass the test (see the testClass below), if not for that, I would have never implemented an Interface, and would have wrote the code in a single class. My point here is: I create a lot of productive code (overhead?), just to pass my written tests. At this point I am asking myself: are my written tests maybe bad or inefficient, so they force me to write more code?

    ConfigurationTest

    public class ConfigurationTest { private HashMap<String, ArrayList<String>> cfg; @Before public void initializeConfig() { cfg = Configuration.initializeConfiguration((keys) -> { HashMap<String, ArrayList<String>> keyValuePairs = new HashMap<>(); ArrayList<String> values = new ArrayList<>(); values.add("127.0.0.1:8080"); keyValuePairs.put(keys.get(0), values); values = new ArrayList<>(); values.add("C:\\file0.txt"); values.add("C:\\file1.txt"); keyValuePairs.put(keys.get(1), values); return keyValuePairs; }, new ArrayList<>(Arrays.asList("BINOCULARIS_SERVER", "BINOCULARIS_FILES"))); } @Test public void testIfAllEnvironmentVariablesAreFound() { HashMap<String, ArrayList<String>> keyValuePairs = new HashMap<>(); ArrayList<String> values = new ArrayList<>(); values.add("127.0.0.1:8080"); keyValuePairs.put("BINOCULARIS_SERVER", values); values = new ArrayList<>(); values.add("C:\\file0.txt"); values.add("C:\\file1.txt"); keyValuePairs.put("BINOCULARIS_FILES", values); assertEquals(cfg, keyValuePairs); } @Test public void testIfAllEnvironmentVariablesAreNotNull() { cfg.forEach((key, value) -> assertNotNull(value)); } } 

    Could someone review the above code and tell me how I could to better?


    Following up is another case, where I have some questions regarding it. The class WebSocket creates a connection, to communicate with another system.

    WebSocket.java

    import org.glassfish.tyrus.client.ClientManager; import javax.websocket.*; import java.net.URI; import java.util.concurrent.CountDownLatch; class WebSocket { private CountDownLatch latch; private boolean sessionOpen = false; Boolean connect(String server) throws Exception { try { latch = new CountDownLatch(1); ClientManager.createClient().connectToServer(new Endpoint() { @Override public void onOpen(Session session, EndpointConfig config) { sessionOpen = true; latch.countDown(); } }, ClientEndpointConfig.Builder.create().build(), new URI(server)); latch.await(); } catch(Exception e) { e.printStackTrace(); } return sessionOpen; } } 

    Given that for testing this class I would need an integration test (since the WebSocket needs a backend), I used Mockito to mock the backend, like this:

    import static org.junit.Assert.*; public class WebSocketTest { @Test public void testIfConnectionCanBeEstablished() throws Exception { WebSocket mockedWebSocket = mock(WebSocket.class); when(mockedWebSocket.connect("ws://127.0.0.1:8080")).thenReturn(true); assertTrue(mockedWebSocket.connect("ws://127.0.0.1:8080")); } } 

    Am I doing this right? Given that I mock(fake) the backend, I consider this not really a test...but how could I test this otherwise? Besides that, the class WebSocket.java overrides methods like onOpen(line 16) - should I test these? If yes, how would I do that?

    Thank you for your time!

    submitted by /u/Fasyx
    [link] [comments]

    [C#] Issues with trying to do maths with some VERY big numbers. Any help would be appreciated.

    Posted: 28 Apr 2018 05:38 AM PDT

    TL;DR: This: https://pastebin.com/r7SyamF4 code isn't working, pls help :(

    So the numbers I am using are the factorials of 365 (and eventually the factorial of 32,768 if I can get this code working). I used wolfram alpha originally, but due to being in an environment with crap internet (I think), wolfram alpha never actually finished calculating. The formula I originally calculated is what is known as the 'Birthday Paradox'. And aims to find the probability that out of n people in a room, at least 2 share a birthday. The formula is as follows:

    P(n) = 1 - (365! / ((365n) * (365-n)!))

    In code, this looks like this (semicolons and Console.WriteLine not included):

    1 - Factorial(365)/(Math.Pow(365, n) * (Factorial(365 - n))) 

    The Factorial() is my own function which seems to work for all of my other calculations. The link to the function is here: https://pastebin.com/yKgxSxQN

    I intend to change the '365' and 'n' constants to considerably larger: '32768' and '500' constants so it would be ace if I could get the smaller examples working first. When I compile my current code (beware for there are an ungodly amount of brackets): https://pastebin.com/bbqe8g6j it returns NaN.

    So I started using the BigInteger variable thing (sorry I can't really think of what to call it in my time of need), and the code started returning '0'. I know for a fact the answer is approximately 0.501, and am confused as to why I am getting 0.

    I ended up just calculating the numerator and denominator on their own and got these outputs: https://pastebin.com/03RHbS04 .

    ...

    So as of half way through this post WolframAlpha started working again and I was able to calculate the equation even after substituting the 365 and n with 32768 and 500. However, I am still curious as to why my code isn't working so I will continue.

    My current code after a bit of fiddling looks like this: https://pastebin.com/r7SyamF4 . Note that I found what looked like a cleaner Factorial function online and used that, if someone could comment on whether or not it is actually better than the for loop I was using before, that would be great.

    Thanks for reading, and thanks for anyone who offers their help.

    submitted by /u/aidenm125
    [link] [comments]

    I need to connect a DB table to 2 other tables with a relationship, do I need 2 relationships?

    Posted: 28 Apr 2018 08:11 AM PDT

    I have 3 tables called:

    Tournament

    Sponsor

    Team

    a Sponsor can sponsor a team

    a Sponsor can sponsor a tournament

    both relationships are many to many, so do i need two relationships with almost identical meaning?

    submitted by /u/xixhxix
    [link] [comments]

    Advice on animating an object using a 'move' method?

    Posted: 28 Apr 2018 02:53 AM PDT

    Hi There,

    I need help on animating an object to move right across the screen around 35 pixels at a time every 10 seconds (this could be in one smooth movement or move all at once every 10 seconds) before moving up the screen once three times. I want to run this using a method called 'void move()' that I have already created.

    Main Code

    Object Code (With empty void move() method

    Animation Plan

    submitted by /u/zootlad
    [link] [comments]

    Task-Based Parallelism

    Posted: 28 Apr 2018 03:02 AM PDT

    Hi, I am trying to implement task based parallelism in my Mandelbrot program. I was just wondering if any one knows of a good tutorial explaining how to do this and/or a sample code which is easy to understand?

    submitted by /u/bllueace
    [link] [comments]

    When to check if file is empty before reading from said file?

    Posted: 28 Apr 2018 02:26 AM PDT

    Hi!

    I have a function that calls another function which reads information from a file. I'm wondering, at which point should I call If (File.Exists(myFile){ -my code here- }. In the first function, before the second one gets called, or within the second function?

    Here's what I mean:

    Calling it in the first function which calls the second function: (FileHandler is the class the function and json file are in)

    private Boolean ContainsName(String name) { string jsonString = ""; if (File.Exists(FileHandler.JsonFile)) { jsonString = FileHandler.ReadAllFromJson(); } ------ The rest of my code ------ } 

    Or calling it in the second function:

    public static String ReadAllFromJson() { string info = ""; if (File.Exists(JsonFile)) { using (StreamReader reader = new StreamReader(JsonFile)) { info = reader.ReadToEnd(); } } return info; } 

    Or should it be included in both? If so, why?

    submitted by /u/Zecuel
    [link] [comments]

    No comments:

    Post a Comment